Getting started with Express.js
Lets create a sample app using express.js, objective of this app will be
- Starts a HTTP server
- Listen to a local port
Lets create a file firstapp.js with below code.
var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Welcome to Salesforcedrillers'); }) var server = app.listen(8080, function () { var host = server.address().address //will fetch the server address var port = server.address().port // will fetch the running port console.log("Example app listening at http://%s:%s", host, port) })
As we can see, express is imported using require(‘express’) into our program. Express (app.listen) will start the server and listen to port 8080. server.address().address will fetch the server address. server.address().port will fetch the port.
We can run the application using the below command.
node firstapp.js