4
I am studying Nodejs for academic purposes and during the implementation of a project I came across this command. What it serves?
4
I am studying Nodejs for academic purposes and during the implementation of a project I came across this command. What it serves?
4
Node.js is an engine, this function can be any "lib", in case it is probably from Express, which is a lib that can be used together with Nodejs
In case you must have misspelled, must refer to .send()
, in an example:
var express = require('express'); //Importa a lib express
var app = express();//Inicia a lib
app.get('/', function(req, res) {
res.send('Olá mundo');
});
Every argument has a meaning:
req
is the HTTP request interface, you can pick up the header sent by client/browser, for example you can pick up an uploadres
is the HTTP response interface, you can send headers and the body to the browser.The req
(requisition) and res
(answer) in app.get('/', function (req, res) {
, are the same objects that Node provides, so it is possible to call the req.pipe()
, req.on('data', callback)
and anything else I’d like to do without the involvement of Express.
More details by clicking on the tag: express
Note that their names may change in function(req, res)
, can do so:
app.get('/', function(foo, bar) {
bar.send('Olá mundo');
});
So .send()
sends something to the HTTP response that you want, for example if you do this:
app.get('/foo/bar', function(req, res) {
res.send('Olá mundo');
});
When accessing the url http://meusite.com/foo/bar
you will see it on the page:
Olá mundo
The HTTP response that will arrive in the browser should be something like:
HTTP/1.1 200 OK
Date: Sun, 10 Oct 2017 23:26:07 GMT
Content-Length: 10
Content-Type: text/html
Olá mundo!
The first line break, after the last header is what divides the body header
Then this HTTP response will be rendered by the browser, it is important to understand this because the HTTP communication works with these two steps, that is to say that the websites work, the browser sends a request the server/script processes and returns a response (you programmed), something like this:
Express has another method called .render
for popular views and send as response, this does not necessarily have to do with http because it occurs before, that is to say it is processed on the server.
Before Express can render template files, the following application settings must be made:
views
is the directory where template files are located, for example:
app.set('views', './views')
view engine
is the model mechanism to be used, for example:
app.set('view engine', 'jade')
Then install the package npm
corresponding to the model mechanism, type this in the terminal/cmd:
npm install jade --save
After the display engine is configured, you do not need to specify the mechanism or load the model engine module into your application; Express loads the module internally as shown below (for the above example).
app.set('view engine', 'jade');
Create a Jade template file called index.jade
in the views directory, with the following content:
html
head
title!= title
body
h1!= message
Then create a route to render the file index.jade
. If the property view engine
not configured, you need to specify the view file extension. Otherwise, you can omit it.
You can create a structure to pass the data like this { title: 'Meu titulo', message: 'Olá mundo!'}
, should look something like this:
app.get('/', function (req, res) {
res.render('index', { title: 'Meu titulo', message: 'Olá mundo!'});
});
When making a request to the home page, the file index.jade
will be rendered as HTML.
-3
@Monitorlpi
Basically this is code to run a Node server on port 3000:
const express = require('express')
const app = express()
app.get('/', (req, res) => {
res.send('<h1>Olá mundo</h1>')
})
app.listen(3000, (err) => {
if (err) {
console.log('Não foi possível rodar o servidor do JobFy');
}else {
console.log('Servidor rodando');
}
})
This part of the send() method can be put into a variable yes, but has to be the whole function. Ex:
const server = app.get('/', (req, res) => {
res.send('<h1>Olá mundo</h1>')
})
then in the part where the server is listening to the request to reply, you can use so:
server.listen(3000, (err) => {
if (err) {
console.log('Não foi possível rodar o servidor do JobFy');
}else {
console.log('Servidor rodando');
}
})
Note that the send method was inside "server". "send()" is an upload. You can use 0 "end()" as well, but html does not have "UTF-8 charset". Usually the guys use the render().
Browser other questions tagged javascript node.js express
You are not signed in. Login or sign up in order to post.
got it. But please take another question, this reply sent by . send() to the view can be "assigned" to some variable in the view?
– MonitorLPI
@Monitorlpi . send has nothing to do with view, maybe you’re talking about
res.render
, which is another function: https://github.com/expressjs/expressblob/master/lib/response.js#L985, is this?– Guilherme Nascimento
I’m referring to . send() himself. Well, in the answer you said that send() sends to the HTTP response what you want and that this answer is that it appears on this page, so I thought that response could be assigned somewhere. I was confused about why to execute this command to send an answer since the reply page will be "blank" only with this answer sent, but I think this is already content for another question. Thank you for answering and for your patience.
– MonitorLPI
@Monitorlpi edited the answer, added about the
Views no Express
, in this example use Jade.– Guilherme Nascimento
@Monitorlpi should do without Jade, as soon as I can test I put an example.
– Guilherme Nascimento
@Guillhermenascimento
+1
. I suggest you changejade
forpug
. The Jade was abandoned– Sergio