What is Express js?
It is a framework for node js written over node js.
Creating a server in node js is very time taking as well as it is difficult to understand for beginners.
visual representation
Why Express js?
Express allows developers to focus on business logic. Instead of focusing on details(routing, parsing incoming body requests).
Installing Express and Creating server.
- install express in your project as production dependencies.
npm install --save express
- get required modules - in app.js
const http = require('http)
const express = require('express')
- set the app with the express()
const app = express()
- create server by calling createServer method and pass app as callback handler
const server = http.createServer(app)
server.listen(3000)
run the nodemon server
npm start
If nodemon not installed follow these steps
npm install --save-dev nodemon
make changes to the package.json file - in script, add the main js file where the server is initialized.
in my case main file is app.js
"scripts": {
"start": "node app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
Top comments (1)
Hi. In the spirit of promoting standardization in the JavaScript world, I'll say this much: You can write your ExpressJS code using ES Modules instead of CommonJS modules.
One of the most obvious advantages is that you no longer need to know two different JavaScripts, but you also get a more robust support since you are backed up by a global standard.
For example, CommonJS does not allow top-level awaits, but ES Modules does. Top-level await is one great feature one should not miss.