Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save xyzdata/c3b7d10dc0ca4b7973b4a22014a149e5 to your computer and use it in GitHub Desktop.
Save xyzdata/c3b7d10dc0ca4b7973b4a22014a149e5 to your computer and use it in GitHub Desktop.
express static web server & Linux node.js

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