Enter data in server response node.js

1

Next, I seamlessly rode the example of hello world in node.js made available in express.js, access it normal in my localhost: 7000.

Is it possible to manipulate hello World's response, such as font size, centering 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/");
    
asked by anonymous 28.04.2018 / 23:44

1 answer

0

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

Sample Expressjs:

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

In the example above the content inside the Html file will be loaded into the html variable. then the res.send method sends the content. Remember the above code should be placed on a route.

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

Example:

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

When accessed by the browser localhost:7000/ it will deliver the content within the directory defined in .static()

Reference: Delivering static files to Express

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

    
29.04.2018 / 00:13