express server port code example

Example 1: SERVER PORT NODE.JS

const http = require('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');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

Example 2: require express server.js

const express = require('express')const app = express() app.get('/', function (req, res) {  res.send('Hello World')}) app.listen(3000)

Example 3: express get port from request

const proxyHost = req.headers["x-forwarded-host"];
const host ? proxyHost : req.headers.host;