Header Ads

Creating a project in node

Creating a new project in node.js


Node is mainly used for server development, it will be more scalable and easy to compile in the chrome runtime environment. it will be used to write a new API to fetch the data from the backend. It will event-driven, non-blocking and single-threaded features.


Installing the node

First of all, install a node, to use the npm(node package manager). so it will be used as creating server-side dependencies(nodeJS). use the recommended package.

Creating a new project.

Open a terminal, create a new directory and name it as a project-x. Then run the command to create a 
package.json automatically.

npm init


Install an express

Express.js has used a lightweight application, used to align the node.js in MVC architecture on the server-side. create a template and deploy it. Using the different database for the connection. 

npm install express

const express = require('express');
const parser = require('body-parser');
const port = 3000;
app.use(parser.json())
app.use(
       parser.urlencoded({
                 extend: true
        })
);
app.listen(port, () => {
         console.log('port', port);
});
Create a new file name as api.js. this will be handle all the API format and pointing the server-side.
const example = (res, res) => {
   const response = { 'status' : '200' };
 
   res.json(response);
};

module.exports = {
  example
}
call the page in the index.js and use the common name for the redirecting URL(/api) and use a different name for APIs.
const express = require('express');
const parser = require('body-parser');
const server = require('./app');
const port = 3000;
app.use(parser.json())
app.use(
       parser.urlencoded({
                 extend: true
        })
);
app.get('/api', server.user);
app.listen(port, () => {
         console.log('port', port);
});

Post a Comment

0 Comments