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;
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:In your case you are responding with a
String:Express will send that as a
"text/html"response: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 theContent-Typeaccording to the file extension. For example this will send a HTML file:However, you are using pug so you already have a view templating library set up.