Skip to content

Instantly share code, notes, and snippets.

@saiumesh535
Created November 2, 2018 10:35
Show Gist options
  • Save saiumesh535/586d44488cee6028a7cd2ed1518de75d to your computer and use it in GitHub Desktop.
Save saiumesh535/586d44488cee6028a7cd2ed1518de75d to your computer and use it in GitHub Desktop.
Serving static HTML files using express node
const path = require('path');
const express = require('express');
const app = express();
const publicPath = path.join(__dirname,'./build');
const port = process.env.PORT || 3001;
app.use(express.static(publicPath));
// write all api routes here
app.get('/hello', (req, res) => {
res.send('hello');
})
// here serve your angular/react html file
app.get('*', (req, res) => {
res.sendFile(path.join(publicPath, 'index.html'));
});
app.listen(port, () => {
console.log('Server is up!');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment