If not using render then which HTML file will be used to display content on the web?

46 views Asked by At

I am creating a project in Node.js. If I am not using render then which HTML file will be used to display content on the web when using res.send('respond with a resource')

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/users', usersRouter);
module.exports = app;

users.js

var express = require('express');
var router = express.Router();

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;
1

There are 1 answers

0
jQueeny On

It won't use any HTML file.

The res.send([body]) function is used to send a HTTP response. The body parameter is flexible enough to respond with a few different things. As per the docs:

The body parameter can be a Buffer object, a String, an object, Boolean, or an Array

In your case you are responding with a String:

res.send('respond with a resource');

Express will send that as a "text/html" response:

When the parameter is a String, the method sets the Content-Type to “text/html”:

If you actually want to send a HTML file then you may find res.sendFile() more helpful as this method looks at the file extension of the given file path and sets the Content-Type according to the file extension. For example this will send a HTML file:

res.sendFile(path.join(__dirname, '/index.html'));

However, you are using pug so you already have a view templating library set up.