The following code will run a minimal node js app. You may use it as a boilerplate for your project.
Copy the following code to your IDE:
// Import required modules
const http = require('http');
// Define port
const PORT = 3000;
// Create HTTP server
const server = http.createServer((req, res) => {
// Set response headers
res.writeHead(200, {'Content-Type': 'text/plain'});
// Send response
res.end('Hello, world!\n');
});
// Start server
server.listen(PORT, () => {
console.log('Server running at http://localhost:${PORT}/');
});
Save it to a file, and call it app.js.
Then you'll be able to run it from your command line (assuming that you have NodeJS installed in your computer:
node app.js
If you want to do the same using Express.JS, you should first install the express package using NPM:
npm init
npm install express
Then create and run an index.js file, similarly that what you did above:
// Import required modules
const express = require('express');
// Create Express application
const app = express();
// Define a route
app.get('/', (req, res) => {
res.send('Hello, world!');
});
// Define port
const PORT = process.env.PORT || 3000;
// Start server
app.listen(PORT, () => {
console.log('Server running on port ${PORT}');
});
And your server is ready to go!
npm start
More about Minimal NodeJS App:
For additional information, you may search for the term "Minimal NodeJS App" in external resources:
- • Google (search engine): Google.com/minimal_nodejs_app
- • Bing (search engine): Bing.com/minimal_nodejs_app
- • Wikipedia (encyclopdia): En.Wikipedia.org/minimal_nodejs_app
Entry Rating:
3.9 out of 5 (total 3 votes)
Your rating:
Additional information:
Search for another phrase
Search for another phrase
What others contributed:
Recently edited entries
Recently edited entries
1. Very useful, thanks.