Skip to content

Instantly share code, notes, and snippets.

@xgqfrms-GitHub
Last active October 5, 2023 15:40
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save xgqfrms-GitHub/7697d5975bdffe8d474ac19ef906e906 to your computer and use it in GitHub Desktop.
Save xgqfrms-GitHub/7697d5975bdffe8d474ac19ef906e906 to your computer and use it in GitHub Desktop.
express static server (Node.js HTTP Server)

Node.js HTTP Server

/**
* express static server for react build/dist test!
*/ 

// simple express server for HTML pages!
// ES6 style

const express = require('express');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
const app = express();

let cache = [];// Array is OK!
cache[0] = fs.readFileSync( __dirname + '/index.html');
cache[1] = fs.readFileSync( __dirname + '/views/testview.html');

app.get('/', (req, res) => {
    res.setHeader('Content-Type', 'text/html');
    res.send( cache[0] );
});

app.get('/test', (req, res) => {
    res.setHeader('Content-Type', 'text/html');
    res.send( cache[1] );
});

app.listen(port, () => {
    console.log(`
        Server is running at http://${hostname}:${port}/ 
        Server hostname ${hostname} is listening on port ${port}!
    `);
});
@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

@xgqfrms-GitHub
Copy link
Author

express static web server & Linux node.js

image

// simple express server for HTML pages!
// ES6 style

const express = require('express');
const fs = require('fs');
const hostname = '127.0.0.1';
const port = 3000;
const app = express();

let cache = {};// object is OK!
cache[0] = fs.readFileSync( __dirname + '/index.html');
cache[1] = fs.readFileSync( __dirname + '/views/testview.html');

app.get('/', (req, res) => {
    res.setHeader('Content-Type', 'text/html');
    res.send( cache[0] );
});

app.get('/test', (req, res) => {
    res.setHeader('Content-Type', 'text/html');
    res.send( cache[1] );
});

app.listen(port, () => {
    console.log(`
        Server is running at http://${hostname}:${port}/
        Server hostname ${hostname} is listening on port ${port}!
    `);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment