create basic express server code example
Example 1: how to make an express server
// this is your code
// ZDev1
// first you should install express in the terminal
// `npm i express`.
const express = require('express');
const app = express();
// route
app.get('/', (req,res)=>{
// Sending This is the home page! in the page
res.send('This is the home page!');
});
// Listening to the port
let PORT = 3000;
app.listen(PORT)
// FINISH!
Example 2: simple express server
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.send('<h1>Some HTML</h1>');
res.send('<p>Even more HTML</p>');
});
app.listen(PORT, () => console.log(`Server is listening on port ${PORT}`));
Example 3: create express server local
// create directory
//npm init -y
//npm i express --save
//create public directory
//create server.js
// <---- In the server js file --->
'use strict';
const express = require('express');
const app = express();
app.use(express.static('public'));// to connect with frontend html
app.use(express.json());//body parse
app.get('/', function(req,res){
res.send('This is the Homepage');
//res.sendFile('index.html');
});
app.listen(3000);