This content originally appeared on DEV Community and was authored by pritudev
Easiest way to create a basic webserver using express js.
We can use node js to create a web server using node js will a long time to create a server so we use express js for these types of thing.
First of all we are gonna create a new file named server.js
or you can use any name you want.
Coding Time
const express = require('express') // npm install express
const app = express()
const port = 3000
// Send text
app.get('/', (req, res) => {
res.send('Hello World!')
})
// Send HTML File
app.get('/', (req, res) => {
res.sendFile('./index.html')
})
app.listen(port, () => {
console.log(`Your app is listening to port ${port}`)
})
Now run your code but how? lemme tell you open terminal in visual studio code or or open command promt in your folder then write down these commands in terminal.
First command
npm install express
Second command
npm install --save-dev nodemon
# To run our code we use this package.
Last command
nodemon server.js
# make sure to replace server.js with your file name
And thats for this post see you in another one
- this was originally took from pritudev.me
This content originally appeared on DEV Community and was authored by pritudev
pritudev | Sciencx (2021-09-25T14:04:14+00:00) Create a basic web server using Express js & HTML. Retrieved from https://www.scien.cx/2021/09/25/create-a-basic-web-server-using-express-js-html/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.