Insert data into answer server Node.js

Asked

Viewed 318 times

1

Following people, I ran the example of hello world in Node.js provided in express.js, access it normal on my localhost:7000.

It is possible for me to manipulate the hello World response, such as font size, centered etc...and display an image together?

Code

// Carregue o módulo http para criar um servidor HTTP.
var http = require('http');

// Configura nosso servidor HTTP para responder com Olá Mundo a todas as solicitações.
var server = http.createServer(function (request, response) {

  // Define os parâmetros de cabeçalho de resposta
  response.writeHead(200, {"Content-Type": "text/plain"});
  // Envia uma resposta para o cliente com a mensagem Hello World
  response.end("Hello World\n");

});

// Define a porta 8000 onde será executado, o ip padrão é 127.0.0.1 / localhost
server.listen(3000);

// Imprime uma mensagem no servidor
console.log("Server running at http://localhost:3000/");

1 answer

0


You can. the method res.send is used to send text in the body of the reply to the client. This text can be JSON, XML... To render pages using the method res.render

expressjs example:

res.render('arquivoHTML',(err, html) => {
  res.send(html);
});

In the above example the contents of the HTML file will be loaded into the html variable. then the method res.send sends the content. Remember the code above should be placed in a route.

To serve a directory with static content (index.html,css,js), like apache or Nginx use the express Static middleware.

Example:

const express = require('express')
const app = express()
app.use('/',express.static('diretório'))
... 

When Access Via Browser localhost:7000/ it will deliver the content inside the directory defined in .static()

Reference: Delivering Static Files to the Express

app.render(view, [locals], callback)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.