Getting Started - NodeJs Project - Making of REST API's

Getting Started - NodeJs Project - Making of REST API's
 

What is called NodeJs?

    Node.js is an open-source, cross-platform, JavaScript runtime environment (Framework) that executes JavaScript code outside a web browser. Node.js lets developers use JavaScript to write command-line tools and for server-side scripting—running scripts server-side to produce dynamic web page content before the page is sent to the user's web browser. Consequently, Node.js represents a "JavaScript everywhere" paradigm, unifying web application development around a single programming language, rather than different languages for server- and client-side scripts.
(Source: Wikipedia)
Getting Started - NodeJs Project - Making of REST API's

Steps to start a NodeJs project:

STEP 1:  Go to the project folder and open CMD.

STEP 2: Run npm init in command prompt.
                 You have to fill the asking questions in CMD properly to generate the package.json file.

STEP 3: Open VS Code or your favorite editor and create the index.js file in the project folder.

STEP 4: Run npm install express body-parser in CMD.

STEP 5: In index.js import express and body-parser module.


var express = require('express');
var bodyParser = require('body-parser');


note: You want to know more about these two modules, google it before you start this project.

STEP 6: Express is a web application framework of node js. It is using here to creating web services and body-parser is a middleware used here to parse the data into JSON in the HTTP requests.


const app = express();
app.use(bodyParser.json({ limit: 1024 * 1024 * 1000, }));


STEP 7: We have to set one port for endpoints, Using that port we can able to access our services. So the link looks like http://localhost:8080


app.listen(8080function () {
    console.log('Node app is running on port:' + 8080);
});


STEP 8: export your app in index.js


module.exports = app;


STEP 9: Most of the tutorials shown some basic working module of node js projects. But here I have shared a somewhat structured one.

                                index.js
                        routes/ routes.js
                        controller/ controller.js
  • routes.js -The file to handling your routes.
  • controller.js - The file to handling service modules respective to the requested routes.

STEP 9: Import routes.js in index.js and pass our express app to route.js module.


require('./routes/routes.js')(app);

   
STEP 10: routes.js
 

const controller = require('../controller/controller')

module.exports = (app=> {
    app.get('/getdata'controller.sendSomeInfo)
}

 
STEP 11: controller.js


exports.sendSomeInfo = (reqres=> {
    
    res.send({ "message": "Hi, am reached" })

}


STEP 12:  Run npm start in your CMD.

STEP 13: The URL to access this service -  http://localhost:8080/getdata
                Open this link in your browser,  you will get this ...HURRAY !!!!

                      






Comments