Hello World with Express
suggest changeThe following example uses Express to create an HTTP server listening on port 3000, which responds with “Hello, World!”. Express is a commonly-used web framework that is useful for creating HTTP APIs.
First, create a folder such as myApp. In that folder, save the following code as hello.js and install Express with npm install express (--save has been the default since npm 5).
// Import the top-level function of express
const express = require('express');
// Creates an Express application using the top-level function
const app = express();
// Define port number as 3000
const port = 3000;
// Routes HTTP GET requests to the specified path "/" with the specified callback function
app.get('/', function(request, response) {
response.send('Hello, World!');
});
// Make the app listen on port 3000
app.listen(port, function() {
console.log('Server listening on http://localhost:' + port);
});
From the command line, run the following command:
node hello.js
Open your browser and navigate to http://localhost:3000 or http://127.0.0.1:3000 to see the response.
For more on Express, see Web Apps With Express.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents