Building an Express Application Chapter 2
- Rename the files in the views folder:
error.jade to error.pug
index.jade to index.pug
layout.jade to layout.pug - Finally, in app.js, remove the line which says:
app.set('view engine', 'jade');
- Add the following line to use pug as the view engine:
app.set('view engine', 'pug');
morgan: This is middleware for logging the HTTP requests
serve-favicon: This is for displaying a favicon in the browser to identify our
application
It's not necessary to have all these dependencies for our application. They
come from installing Express.js. Just dig around for what you want and
then add or remove the packages as per your application needs.
For now, we will leave it as it is. The express command just adds the dependencies to our
package.json file and creates a skeleton for our application. In order to actually install
these modules and packages listed in the package.json file, we need to run:
$ npm install
This command will actually install all the dependencies. Now, if we look into the folder
structure, we can see a new folder is being added called node_modules. This is the place
where all of the packages that we installed within that application reside.
Now, the first thing that we want to do is to set up a web server. For that, add the following
line in the app.js file:
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development'? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});