how to create express server code example
Example 1: express.js server
const express = require("express");
const app = express();
app.get("/", (req, res) => {
res.send("This is home page.");
});
app.post("/", (req, res) => {
res.send("This is home page with post request.");
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server is running on PORT: ${PORT}`);
});
Example 2: how to make an express server
const express = require('express');
const app = express();
app.get('/', (req,res)=>{
res.send('This is the home page!');
});
let PORT = 3000;
app.listen(PORT)
Example 3: express js 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 4: require express server.js
const express = require('express')const app = express() app.get('/', function (req, res) { res.send('Hello World')}) app.listen(3000)
Example 5: js express server
const http = require('http')
const express = require('express')
const app = express()
const server = http.Server(app)
app.set('port', 8888)
server.listen(8888)
app.get('/', (req, res) => {
res.json({teste: true})
})
Example 6: create express server local
'use strict';
const express = require('express');
const app = express();
app.use(express.static('public'));
app.use(express.json());
app.get('/', function(req,res){
res.send('This is the Homepage');
});
app.listen(3000);