Monday 4 February 2019

Express module in React.Js

How to install modules in Node.Js?
Either through wizard or from package.json we can add wanted modules in node.js project. I have done it through wizard and you may get reflation in package.json.




Package.json
"devDependencies": {
    "@types/node""^8.0.14",
    "express""^4.16.4"
  }


What is Express modules in Node.Js?
It use connect middle-ware that interact with core http module. So either we work with http module or can have express module in application that help us to configure endpoint, route and more rapidly. More about express could be found here .


How to use express module in node.js?
We just need to add module in project and then can call it in anywhere, like I have use in server.ts.

//load installed module 'express'.
var express = require('express');
 
//initialized express and stored in a variable app.
var app = express();
 
//Created a key having name 'port' with default process value.
app.set('port', process.env.PORT || 1337);
 
 
//Created rout for root '/'
app.get('/'function (req, res) {
    res.send('<html><body><h1>Hello World</h1></body></html>');
});
 
 
 
//Created rout for root '/about'
app.get('/about'function (req, res) {
    res.send('<html><body>My name is raj.</body></html>');
});
 
 
//We can have different verbs like get, post, delete and more.
app.post('/postdata'function (req, res) {
    res.send('POST Request');
});
 
app.put('/updatedata'function (req, res) {
    res.send('PUT Request');
});
 
 
app.delete('/deletedata'function (req, res) {
    res.send('DELETE Request');
});
 
//Again instead of http.createServer module we use app.listen to configure endpoint of App.
var server = app.listen(app.get('port'), function () {
    console.log('Node server is running..');
});



Output in browser:
For root '/'


And for '/about'