How Set Up Express.js App

Abinash Phulkonwar
3 min readApr 21, 2021

--

First, you need to download NODE JS from https://nodejs.org/en/ and install it on your local machine and then Download Visual Studio Code from https://code.visualstudio.com/download base on your OS and install it.

Install Express.js

Make a folder called whatever you want e.g. “first express app” link this

open VS code in this folder by right click and select the Open With Code option or manually go to the folder.

and open a new terminal (open terminal by “Ctrl + Shift + ` ” in Windows)

and initialize your app with “npm init”. For initializing type “npm init” in your terminal and enter and give data. This will create a package.json file. Which contain your app-related data.

npm init and package.json file

And then install “Express.js”. To install “Express js” in your terminal “npm install express” and enter that will install “express js” and dependencies of the express. “npm install express” will create a node modules folder where all your dependencies package are stored.

And in the “package.json” file in scripts write “start”: “node app.js”. this will help you to start your express server.

Create app.js file in your rote directory, in app.js file(this file name need to same with package.json “main” key stored file name), required to “express js” by require(“express”). and initialize express app and listen to your app in a specific port.

const express = require('express'); // require expressconst app = express(); // initalized expressapp.listen(3000); // app listen port 3000 or any port you wany

And serve “HTML” file:

const express = require('express'); // require expressconst app = express(); // initalized expressapp.get('/', (req, res) => {     res.sendFile(path.join(__dirname, "view/home.html"));});app.listen(3000); // app listen port 3000 or any port you wany

app.get(‘/’)” means if the server gets a “GET” request on the “/” then the server sends the “home.html ” file to the client browser.

To serve the static files such as javascript files, CSS files, and images. You need to make a folder called “static” or whatever you want where all of your static files stored.

To sends, static files use the “express.js” built-in middleware function called

“express.static” :

const path = require('path');// path.join method joins all given path segments together using the // platform-specific separator
// __dirname give the path of the folder where the javascript
// file stored
app.use(express.static(path.join(__dirname, "public")));

All Code put together here:

const express = require('express'); // require expressconst path = require('path'); // require path moduleconst app = express(); // initalized expressapp.use(express.static(path.join(__dirname, "public")));app.get('/', (req, res) => {res.sendFile(path.join(__dirname, "view/home.html"));});app.listen(3000); // app listen port 3000 or any port you wany

Now you have basic knowledge of “how to set up express.js app”

Note: This “express.js” server set up only for a development environment.

Thank you!

--

--

No responses yet