What is the command "res.send ()" in Express?

2

I am studying NodeJS for academic purposes and during the implementation of a project I came across this command. What is it for?

    
asked by anonymous 09.10.2017 / 00:42

1 answer

2

Node.js is an engine, this function can be any "lib", in the case probably it is Express, which is a lib that can be used together with NodeJS

In case you should have spelled wrong, it should 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');
});

Each argument has a meaning:

  • req is the HTTP request interface, you can get the header sent by the client / browser, for example you can get an upload
  • res is the HTTP response interface, you can send headers and body to the browser.
The req (request) and res (response) in app.get('/', function (req, res) { , are the same objects that Node provides, so you can call req.pipe() , req.on('data', callback) and anything else you would like do without Express's involvement.

  

More details by clicking on the tag:

Note that their names may change in function(req, res) , you can do this:

app.get('/', function(foo, bar) {
  bar.send('Olá mundo');
});

Then .send() sends the HTTP response something you want, for example if you do this:

app.get('/foo/bar', function(req, res) {
  res.send('Olá mundo');
});

When you access the url http://meusite.com/foo/bar you will see this 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 header of the body

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, ie this is how the sites work, the browser sends a request the server / script processes and returns a response (which you have programmed), something like this:

Views on Express

Express has other methods called .render for popular views and send as a reply, this does not necessarily have to be with http because it occurs before, ie it is processed on the server.

Before Express can render model files, the following application settings must be made:

views is the directory where the template files are located, for example:

app.set('views', './views')

view engine is the template engine to be used, for example:

app.set('view engine', 'jade')

Then install the npm package corresponding to the template engine, enter this in the / cmd terminal:

 npm install jade --save

After the display engine is configured, you do not need to specify the engine or load the model engine module in your application; Express loads the module internally, as shown below (for the example above).

app.set('view engine', 'jade');

Create a Jade template file named 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 view engine property is not configured, you must specify the extent of the view file. Otherwise, you can omit it.

You can create a structure to pass the data so { 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 you make a request to the home page, the index.jade file will be rendered as HTML.

    
09.10.2017 / 00:55