To create a web server using Node.js
, you can follow these steps:
- Install
Node.js
on your computer. - Open a command prompt and run the
"npm init"
command. This will generate thepackage.json
file for your project. - Install the
Express.js
library by running the command"npm install express"
. - Copy the following code into a JavaScript file and run the file:
const express = require('express')
const app = express()
const port = 3000
app.get('/', (req, res) => res.send('Hello World!'))
app.listen(port, () => console.log(`Example app listening on port ${port}!`))
This code creates a web server and when you go to "http://localhost:3000" in your browser, "Hello World!" displays the text.