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/javascript/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 globallyNote: 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 scaffoldedProject 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 seenExplanation: 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
Express app.get() Request Function The app.get() function is used to define routes on your server that handle HTTP GET requests. A GET request is typically used when the client asks the server to send back some information, like retrieving a webpage or data from a database.Itâs an essential part of building websites and APIs, as it a
6 min read
Express app.use() Function The app.use() function in Express.js adds middleware to the application's request-processing pipeline. It applies the specified middleware to all incoming requests or to specific routes, allowing you to modify request/response objects, perform operations, or handle errors throughout the application.
3 min read
Introduction to Express Prerequisite - Node.js What is Express? Express is a small framework that sits on top of Node.js's web server functionality to simplify its APIs and add helpful new features.It makes it easier to organize your application's functionality with middle ware and routing; it adds helpful utilities to Nod
2 min read
Express res.locals Property The `res.locals` property is an object that holds response local variables specific to the current request. It has a scope limited to the request and is accessible only to the view(s) rendered during that particular request/response cycle, if any. Syntax:res.localsParameter: No parameters. Return Va
2 min read
Controllers in ExpressJS Express.js is one of the most popular frameworks for Node.js, enabling developers to build web applications and APIs efficiently. In fact, over 70% of Node.js web applications use Express.js due to its minimalistic yet powerful design. As applications grow in complexity, organizing code becomes esse
4 min read
How to use TypeScript on backend ? TypeScript, developed by Microsoft, enhances JavaScript by adding type safety, which significantly simplifies debugging and development. This guide will walk you through setting up a TypeScript backend with Node.js and Express, showing you how to leverage TypeScriptâs robust features to prevent comm
2 min read