DEV Community

Emmanuel Amoo
Emmanuel Amoo

Posted on • Originally published at bandojay.hashnode.dev on

Create a Web Server in Node.js using Express and the ES6 Module

When viewing a webpage in a browser, a request is made to another computer, which then generates a webpage for you as a response. The computer you send your request to is known as a web server. When you type a URL into your web browser, your computer sends a request to a web server. The web server then sends back the requested information, which is usually an HTML page or a JSON object. Node.js allows us to write backend codes in Javascript, even though Javascript has traditionally been used to write frontend codes. For this article, well be learning how to build web servers using the ES6 syntax and Express with Node.js.

Prerequisites

To run npm commands on your machine, you must first install Node.js. You can also visit here to download and install Node.js on your machine. In addition, you need to have a good grasp of JavaScript and how to create Node.js applications in particular.

What is ES6?

ES6 is the newly released version of the javascript programming language whose proper name is ECMAScript. ES6 succeeds the previous ES5 version and it was released in 2011. It adds several features to javascript such as classes and others to make development easier. You can read more here

Initializing npm and setting up your package.json to accept ES6

We need to initialize a new package.json file in the root directory of our project file by running the command below in your terminal

npm init -y

Enter fullscreen mode Exit fullscreen mode

With that, wed have initialized a npm package, youll see the package.json file in your folder

In the package.json file, add "type": "module" beneath the main. Adding that line enables the ES6 modules and your package.json file should look like this:

{
  "name": "simple-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^3.0.1"
  }
}

Enter fullscreen mode Exit fullscreen mode

Also, install Nodemon by

npm i -save-dev nodemon

Enter fullscreen mode Exit fullscreen mode

Then, add the following line as a start script in your package.json file

    "start": "nodemon --experimental-modules --es-module-specifier-resolution=node index.js"

Enter fullscreen mode Exit fullscreen mode

Your code should look like this

{
  "name": "simple-server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "nodemon --experimental-modules --es-module-specifier-resolution=node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2"
  },
  "devDependencies": {
    "nodemon": "^3.0.1"
  }
}

Enter fullscreen mode Exit fullscreen mode

Installing Express

To install the express framework to create our servers, run the code below in your terminal

npm i express

Enter fullscreen mode Exit fullscreen mode

Setting up your Index.js file and creating a basic server

The next thing we have to do is to create our index.js file, set up our web server by importing express using the ES6 import syntax, listen to our port and then create a get route to test if our code is working.

First, we import express using the ES6 import option and set up our app

import express from "express";
const app = express();

Enter fullscreen mode Exit fullscreen mode

Next, we create our port and listen to it to get our server running

const PORT = 5000;

app.listen(PORT, () => {
    console.log(`Server is running on Port ${PORT}`)
})

Enter fullscreen mode Exit fullscreen mode

Then, we create a route to test if our server is up and running

app.get('/', (req, res) => {
    res.send('This is my server using the ES6 syntax')
});

Enter fullscreen mode Exit fullscreen mode

Running the Server

Once everything has been set, we need to run our server to check if it actually worked. Run npm start in your terminal and the web server will start listening on port 5000.

Open your browser, then go to http://localhost:5000, you should see a successful response with the message: This is my server using the ES6 syntax.

Running the server

If you have any issues, please double-check that you followed all of the steps correctly. This article should help you to start writing Node.js applications using the ES6 syntax, especially if you are also writing React and switching between ES5 for Node.js and ES6 for React.

Conclusion

This article teaches you how to write ES6 node.js applications without using Babel or other complex dependencies. Node does not directly support ES6 imports and will throw an error if you try to use them. However, by adding the module type and the start script, you can use Node's experimental support for ES modules, which supports ES6 syntax and allows you to type and run your code.

Top comments (0)