Node js Webserver and rest API using express
Express is a open source web application framework for Node.js, It is designed for building web applications and REST APIs for node js.
Express provides a robust set of features to develop web and mobile applications.
We can set up middleware's to respond to HTTP Requests.
Also We can define routing for API.
Its allow to render html content dynamically.
Installing Express
npm install express
Hello world sample API
const express = require('express'); const app = express(); app.get('/', function (req, res) { res.send('Hello World'); }) const PORT = process.env.PORT || 3000; var server = app.listen(PORT , function () { console.log(`Server Started: http://localhost:${PORT}`) })
Save the above code with file name "index.js" and start the server using the command below
node index.js
You will se the output like
Server Started: http://localhost:3000
open the same URL in your browser you can see the result.
app.get('/', function (req, res) { req //it's a client request object Res //it's a server response object })
Comments (0)
Post a Comment
Cancel