Monday, July 19, 2021

Express Hello World

1.  npm install express body-parser morgan

2. Create an html file called index.html in public/views/index.html and simply write text "Hello World!" (no html tags required) . 



var express = require("express");
var bodyParser = require("body-parser");
var morgan = require("morgan");
var config = require("./config")

var app = express();

app.use(bodyParser({extended : true}));
app.use(bodyParser.json());
app.use(morgan("dev"));


app.get('*'function (reqres) {
   res.sendFile(__dirname + "/public/views/index.html");
})

var server = app.listen(config.portfunction (err) {
    if (err
    {
        console.log(err);
    }
    else 
    {
        var host = server.address().address
        var port = server.address().port
   
        console.log("Listening at http://%s:%s"hostport)
    }
})


Config.js: 

module.exports = {
    "database" : "",
    //"port" : process.env.PORT || 3000 , 
    "port" : 3000
    "secretKey" : "MySecretKey"
}

//NOTE : DO NOT USE "port" : process.env.PORT || 3000, 
// => this will give 
//New port every time, hence difficult to use. instead use 
//a fixed port


public/views/index.html :

Hello World!


Start the project by using "node server.js" or "nodemon server.js" 

and navigate to localhost:3000 on your browser 


















No comments:

Post a Comment

How to check local and global angular versions

 Use the command ng version (or ng v ) to find the version of Angular CLI in the current folder. Run it outside of the Angular project, to f...