DEV Community

Cover image for Express JS - part 1
Subrat kumar jena
Subrat kumar jena

Posted on

Express JS - part 1

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

lines of code node vs express

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
Enter fullscreen mode Exit fullscreen mode
  • get required modules - in app.js
const http = require('http)
const express = require('express')
Enter fullscreen mode Exit fullscreen mode
  • set the app with the express()
const app = express()
Enter fullscreen mode Exit fullscreen mode
  • create server by calling createServer method and pass app as callback handler
const server = http.createServer(app)
server.listen(3000)
Enter fullscreen mode Exit fullscreen mode

run the nodemon server
npm start

If nodemon not installed follow these steps

npm install --save-dev nodemon
Enter fullscreen mode Exit fullscreen mode
  • 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"
  },
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

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.