/**
* 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}!
`);
});
Last active
October 5, 2023 15:40
-
-
Save xgqfrms-GitHub/7697d5975bdffe8d474ac19ef906e906 to your computer and use it in GitHub Desktop.
express static server (Node.js HTTP Server)
Author
xgqfrms-GitHub
commented
Nov 28, 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
express static web server & Linux node.js
// 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