This post is based on the “Hello World!” tutorial on the Node.js site [1]. After a couple of days of frustration getting MongoDB configured on my dev box I was overjoyed to find this perfect set of instructions!
In this tutorial:
Required knowledge:
- Installing Node.js on a WAMP box
1. hello-world.js
Create the hello-world.js file and paste the following code into it. Save the file.
const http = require('node:http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain'); res.end('Hello World!\n'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
2. Run in PowerShell
Open PowerShell in your project folder.
Enter the command: node hello-world.js
3. Run in the browser
Open your browser at: http://127.0.0.1:3000/
4. hello-world.js HTML
A quick fiddle and you can output some HTML:
const http = require('node:http'); const hostname = '127.0.0.1'; const port = 3000; const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/html'); res.end('<h1>Hello World!</h1>'); }); server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); });
References:
- Node.js v22.3.0 documentation (no date) Usage and example | Node.js v22.3.0 Documentation. Available at: https://nodejs.org/docs/latest/api/synopsis.html (Accessed: 26 June 2024).