Navigation Menu

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 commented Nov 28, 2016

// 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}!
    `);
});

@xgqfrms-GitHub
Copy link
Author

xgqfrms-GitHub commented Dec 7, 2016

new server with correct path!

html

<base href="/">
<script type="text/javascript" src="inline.bundle.js"></script>

router

/ > /dist/index.html

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

## /inline.bundle.js > /dist/inline.bundle.js

cache[1] = fs.readFileSync( __dirname + '/dist/inline.bundle.js');
app.get('/inline.bundle.js', (req, res) => {
    res.setHeader('Content-Type', 'text/javascript');
    res.send( cache[1] );
});
// simple express server for HTML pages!
// ES6 style

const express = require('express');
const fs = require('fs');
const hostname = '127.0.0.1';
// http://localhost:4200/
const port = 4200;
const app = express();

let cache = {};// object is OK!

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

cache[1] = fs.readFileSync( __dirname + '/dist/inline.bundle.js');
app.get('/inline.bundle.js', (req, res) => {
    res.setHeader('Content-Type', 'text/javascript');
    res.send( cache[1] );
});

cache[2] = fs.readFileSync( __dirname + '/dist/styles.bundle.js');
app.get('/styles.bundle.js', (req, res) => {
    res.setHeader('Content-Type', 'text/javascript');
    res.send( cache[2] );
});

cache[3] = fs.readFileSync( __dirname + '/dist/main.bundle.js');
app.get('/main.bundle.js', (req, res) => {
    res.setHeader('Content-Type', 'text/javascript');
    res.send( cache[3] );
});


app.listen(port, () => {
    console.log(`
        Server is running at http://${hostname}:${port}/ 
        Server hostname ${hostname} is listening on port ${port}!
    `);
});
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=0.5, maximum-scale=3.0">
    <meta name="description" content="Angualr2 App">
    <meta name="author" content="xgqfrms, https://xgqfrms.github.io">
    <title>Ng2App</title>
    <base href="/">
    <link rel="icon" href="favicon.ico" type="image/x-icon">
</head>
<body>
    <app-root>Loading...</app-root>
    <script type="text/javascript" src="inline.bundle.js"></script>
    <script type="text/javascript" src="styles.bundle.js"></script>
    <script type="text/javascript" src="main.bundle.js"></script>
</body>
</html>

basic-routing

https://stackoverflow.com/questions/4720343/loading-basic-html-in-node-js/40844201#40844201

https://expressjs.com/en/starter/basic-routing.html

@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