Setup Express JS
- Goto to an empty folder
- Create a file named
index.js
npm init -y
- Will create package.jsonnpm i express
- Installs express- Pasted the below sample code in
index.js
My own starter index.js
_9const express = require("express");_9const app = express();_9_9const PORT = 4000;_9app.get("/", function (request, response) {_9 response.send("🙋♂️, 🌏 🎊✨🤩");_9});_9_9app.listen(PORT, () => console.log(`The server started in: ${PORT} ✨✨`));
Goto to http://localhost:4000 to see the 🙋♂️, 🌏 🎊✨🤩
message.
Congratulations you created a server 🎉🎉
Manually restart the server
ctrl + c
- Stop servernode index.js
- Start server
Automatic restart of server - nodemon
- Install nodemon -
npm install --save-dev nodemon
--save-dev
flag is mentioned sincenodemon
is only be needed for development
scripts
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}
Terminal commands
npm run start
ornpm start
(shortcut)->
to run/start the appnpm run dev
->
to dev mode- Dev mode automatically listens to changes you make and restarts the server
Latest Import & Exports
_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}
index.js
with latest import syntax
_10// const express = require("express"); // "type": "commonjs"_10import express from "express"; // "type": "module"_10const app = express();_10_10const PORT = 4000;_10app.get("/", function (request, response) {_10 response.send("🙋♂️, 🌏 🎊✨🤩");_10});_10_10app.listen(PORT, () => console.log(`The server started in: ${PORT} ✨✨`));
@ragavkumarv
swipe to next ➡️