Blog

Express

Setup

JS

Setup Express JS

Express JS - Setup the most popular JS framework of backend

Ragav Kumar V

Ragav Kumar V

Aug 29, 2022 — Updated Dec 8, 2022 · 2 min read

  1. Goto to an empty folder
  2. Create a file named index.js
  3. npm init -y - Will create package.json
  4. npm i express - Installs express
  5. Pasted the below sample code in index.js

_9
const express = require("express");
_9
const app = express();
_9
_9
const PORT = 4000;
_9
app.get("/", function (request, response) {
_9
response.send("🙋‍♂️, 🌏 🎊✨🤩");
_9
});
_9
_9
app.listen(PORT, () => console.log(`The server started in: ${PORT} ✨✨`));

Goto to http://localhost:4000 to see the 🙋‍♂️, 🌏 🎊✨🤩 message.

Congratulations you created a server 🎉🎉

  1. ctrl + c - Stop server
  2. node index.js - Start server
  1. Install nodemon - npm install --save-dev nodemon
  2. --save-dev flag is mentioned since nodemon is only be needed for development

Its like desktop shortcut to run commands


_6
{
_6
"scripts": {
_6
"start": "node index.js", // helps in heroku deployment
_6
"dev": "nodemon index.js" // shortcut to run nodemon
_6
}
_6
}

  1. npm run start or npm start (shortcut) -> to run/start the app
  2. npm run dev -> to dev mode
  3. Dev mode automatically listens to changes you make and restarts the server

_7
{
_7
"type": "module", // to support latest import & export syntax
_7
"scripts": {
_7
"start": "node index.js",
_7
"dev": "nodemon index.js"
_7
}
_7
}


_7
{
_7
"type": "commonjs", // older require & module.exports syntax
_7
"scripts": {
_7
"start": "node index.js",
_7
"dev": "nodemon index.js"
_7
}
_7
}


_10
// const express = require("express"); // "type": "commonjs"
_10
import express from "express"; // "type": "module"
_10
const app = express();
_10
_10
const PORT = 4000;
_10
app.get("/", function (request, response) {
_10
response.send("🙋‍♂️, 🌏 🎊✨🤩");
_10
});
_10
_10
app.listen(PORT, () => console.log(`The server started in: ${PORT} ✨✨`));

@ragavkumarv
swipe to next ➡️