Scaffolding an ExpressJS app from scratch
Last Updated :
07 Aug, 2023
Scaffolding is creating the skeleton structure of application. It allows users to create own public directories, routes, views etc. Once the structure for app is built, user can start building it. Express is the open source web development framework for Node.js for building web applications and the APIs. To install express in the Node.js environment use the NPM (Node Package Manager).
Syntax:
npm install express --save
To use express in the Node, use the following syntax:
var express = require('express');
Prerequisites: Node.js should be installed beforehand. Use the link to learn about prerequisites: https://www.geeksforgeeks.org/nodejs-connect-mongodb-node-app-using-mongoosejs/.
Getting Started: Using express-generator package to install ‘express’ command line tool. express-generator is used to create the structure of application.
Installing express-generator:
Steps:
1. Navigate to the folder where app is to be built out using the terminal.
2. Now in the terminal, install express-generator using the following command.
npm install express-generator -g

Installing express-generator globally
Note: npm installs the packages in two ways: locally (default) and globally The packages installed locally are local to the current project and the packages installed globally are global packages which once installed can be used anywhere in your system.
Scaffolding the app: Below image shows the scaffolding of the application. Basic structure of the application is being created if observed. Public directories, paths, routes, views, etc. are being created which would form the structure of application.

express app scaffolded
Project Folder: The project folder is constituted of various folders/files which can be seen in image. Comparing the scaffolding structure and the project structure it can be clearly seen that the folders/files created in structural mode are present in project folder which was the purpose of scaffolding the application.

The GeeksForGeeks sample application that was scaffolded constitutes the image. Various files and folders likes routes, public, package.json etc. can be seen
Explanation: Explaining the files/folders in project.
1. bin: The file inside bin called www is the main configuration file of our app.
2. public: The public folder contains the files which are to be made public for use like JavaScript files, CSS files, images etc.
3. Routes: The routes folder contains files which contain methods to help in navigating to different areas of the map. It contains various js files.
4. views: The view folder contains various files which form the views part of the application.
Example: homepage, the registration page, etc.
Note: The extension of the files at the time of writing this article is .jade. Change these file extensions to .pug as the jade project has changed to pug.
In the app.js file, change the following code:
app.set('view engine', 'jade'); (most probably in line 15)
to :
app.set('view engine', 'pug');
This will change the view engine to pug.
5. app.js: The app.js file is the main file which is the head of all the other files. The various packages installed have to be ‘required’ here. Besides this, it serves many other purposes like handling routers, middle-wares etc.
6. package.json: package.json file is the manifest file of any Node.js project and express.js application. It contains the metadata of the project such as the packages and their versions used in the app (called dependencies), various scripts like start and test (run from terminal as ‘npm start’), name of the app, description of the app, version of the app, etc.
Running the Scaffold app: Install all the dependencies mentioned in the package.json file required to run the app using the following command:
npm install
After the dependencies are installed, run the following command to start the ExpressJs app:
npm start
Similar Reads
Task Scheduling App with Node and Express.js
Task Scheduling app is an app that can be used to create, update, delete, and view all the tasks created. It is implemented using NodeJS and ExpressJS. The scheduler allows users to add tasks in the cache of the current session, once the app is reloaded the data gets deleted. This can be scaled usin
4 min read
Express.js | app.path() Function
The app.path() function returns the canonical path of the app as a string. When mounting advanced apps, the behavior of this approach can become extremely complicated. In most cases, it is better to use req.baseUrl to obtain the app's canonical path. Installation of the express module: You can visit
2 min read
Express.js | app.all() Function
The app.all() function is used to route all types of HTTP requests. Like if we have POST, GET, PUT, DELETE, etc, requests made to any specific route, let's say /user, so instead of defining different APIs like app.post('/user'), app.get('/user'), etc, we can define single API app.all('/user') which
2 min read
Express.js app.locals Property
In Express.js, app.locals is an object used to store variables that need to be available throughout the app. These variables can be accessed globally across different routes or middleware and are often used for configuration settings or values that should be shared What is app.locals?app.locals is a
4 min read
Budget Tracking App with Node.js and Express.js
In this article, weâll walk through the step-by-step process of creating a Budget Tracking App with Node.js and Express.js. This application will provide users with the ability to track their income, expenses, and budgets. It will allow users to add, delete, and view their income and expenses, as we
15 min read
Express.js app.router() Method
Express.js is a Node.js web flexible framework that provides a set of features for mobile and web applications. Express has various methods provided by many developers and one of the methods in Express is a Router which is used to divert the user to different pages of the website base on request. Sy
3 min read
Travel Planning App API using Node & Express.js
In this article, weâll walk through the step-by-step process of creating a Travel Planning App With Node and ExpressJS. This application will provide users with the ability to plan their trips by searching for destinations, booking flights and hotels, submitting reviews, receiving notifications, sha
10 min read
Express app.post() Function
The app.post() function in Express.js handles HTTP POST requests to a specific route. It defines a callback function to process incoming data sent via POST, typically used for submitting forms or sending data to a server from clients. Syntax: app.post(path, callback [, callback ...])Arguments: Path:
2 min read
Express.js | app.route() Function
The app.route() function returns an instance of a single route, which you can then use to handle HTTP verbs with optional middleware. Use app.route() to avoid duplicate route names (and thus typo errors). Syntax: app.route( path )Installation of the express module: You can visit the link to Install
2 min read
Express.js | app.param() Function
The app.param() function is used to add the callback triggers to route parameters. It is commonly used to check for the existence of the data requested related to the route parameter. Syntax: app.param([name], callback) Parameters: name: It is the name of the parameter or an array of them.callback:
2 min read