Developing the application server

Now that we have all the prerequisites set up, create an index.js file inside the root directory:

touch index.js

So far, we have created a lot of files and folders. Your sensor-project directory structure up until this point should look like this:

.
├── hello-world
│ └── hello-world.js
└── server
├── .gitignore
├── index.js
├── package.json
└── node_modules
└── ...
We are now going to write the code to run our server by editing the server/index.js file.

First, we have to import the express library we just installed:

    const express = require('express');

The require function is a part of the node's standard library. It imports the module given as its argument and returns it. In this line, we are importing the express module and assigning it to the express variable.

Whenever we require a node module, the node first checks the node_modules directory in the same directory where the file we are executing is present.

We then create an instance of a server application:

    const app = express()

The express variable is a constructor, which is a function or method that returns an object. In our case, the express constructor returns an instance of an express server application.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.170.183