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 (req, res) {
res.sendFile(__dirname + "/public/views/index.html");
})
var server = app.listen(config.port, function (err) {
if (err)
{
console.log(err);
}
else
{
var host = server.address().address
var port = server.address().port
console.log("Listening at http://%s:%s", host, port)
}
})
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