NodeJS-Express-Handlebars Project
- NodeJS is a JavaScript runtime used in server-side development.
- Express is a Fast, unopinionated, minimalist web framework for NodeJS.
- Handlebars is a simple templating language.
Project Scaffolding
Use Express application generator to generate boilerplate.
npx express-generator
This command will create the following files:
create : myapp
create : myapp/package.json
create : myapp/app.js
create : myapp/public
create : myapp/public/javascripts
create : myapp/public/images
create : myapp/routes
create : myapp/routes/index.js
create : myapp/routes/users.js
create : myapp/public/stylesheets
create : myapp/public/stylesheets/style.css
create : myapp/views
create : myapp/views/index.jade
create : myapp/views/layout.jade
create : myapp/views/error.jade
create : myapp/bin
create : myapp/bin/www
Directories And Files
myapp
This will be the root directory of the project.
package.json
This file holds various meta-data relevant to the project.
public
All the static files such as JavaScript files, stylesheets, images, fonts, and other static files are placed here. Express will make this folder accessible from the URL.
routes
All the application routes are defined here.
views
This folder will contain the template files to generate the HTML files. Express supports many template engines such as pug, ejs, jade, handlebars, etc. The default template engine is jade, this can be change by using the --view
option.
npx express-generator --view=pug
app.js
Express web framework is set up here.
// IMPORTING express
const express = require("express");
// INITIALIZING express APP
const app = express();
// ENABLE JSON PARSER AND DEFAULT SIZE LIMIT IS "100kb"
app.use(express.json());
// PARSES INCOMING REQUESTS WITH URLENCODED PAYLOADS
app.use(express.urlencoded({ extended: false }));
app.get("/", (req, res) => {
res.send("Hello World");
});
module.exports = app;
Modules
helmet
: Help protect the app from some well-known web vulnerabilities by setting HTTP headers appropriately.javascriptconst express = require("express"); const helmet = require("helmet"); const app = express(); // SECURITY HEADERS app.use(helmet()); app.disable("x-powered-by"); app.disable("etag"); app.use(express.json()); app.use(express.urlencoded({ extended: false })); app.get("/", (req, res) => { res.send("Hello World"); }); module.exports = app;