Created
November 2, 2018 10:35
-
-
Save saiumesh535/586d44488cee6028a7cd2ed1518de75d to your computer and use it in GitHub Desktop.
Serving static HTML files using express node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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