Minimal NodeJS App, #minimal_nodejs_app - MareMakom
Minimal NodeJS App


The following code will run a minimal node js app. You may use it as a boilerplate for your project.

A minimal Node.js application can be created using the runtime's native http module without installing any external packages or dependencies. By calling http.createServer(), the script listens for incoming requests on a specified network port and sends back an HTTP response with headers and a body payload.

For production applications requiring complex routing, middleware, and template rendering, developers frequently adopt lightweight web frameworks such as Express.js. Below are standard code snippets for bootstrapping both a zero-dependency native HTTP server and a basic Express server.


Back to Home

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

Main Site for Minimal Node.js App


External websites and official resources for Minimal Node.js App:


For additional information, you may search for the term "Minimal NodeJS App" in external resources:


Entry Rating:   3.9 out of 5 (total 3 votes)
Your rating:

Comments and suggestions:

1. When building an API endpoint, remember to set the Content-Type header to application/json and serialize the payload with JSON.stringify().

2. For deployment on cloud platforms like Heroku or AWS, always bind the listening port to process.env.PORT rather than hardcoding port 3000.

3. In modern Node.js versions (v18.11.0 and later), you can use the built-in --watch flag (node --watch app.js) for automatic restarts during development.

Send a comment (will be published after review):


Additional information:
Search for another phrase
What others contributed:
Recently edited entries