Created
June 2, 2018 17:39
-
-
Save rounakdatta/c9e1b4b91a3a25e9a6ea76de7999dd3f to your computer and use it in GitHub Desktop.
Dead simple Node.js code for serving HTML pages
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 http = require('http'); | |
const fs = require('fs'); | |
const express = require('express') | |
const path = require('path'); | |
const app = express(); | |
app.use(express.static(path.resolve(`${__dirname}/web/public`))); | |
console.log(`${__dirname}/web`); | |
app.use('*', (req, res, next) => { | |
console.log(`URL: ${req.baseUrl}`); | |
next(); | |
}); | |
app.use((req, res, next) => { | |
res.header('Access-Control-Allow-Origin', '*'); | |
res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS'); | |
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept,X-access-token'); | |
next(); | |
}); | |
app.use((err, req, res, next) => { | |
if (err) { | |
res.send(err); | |
} | |
}); | |
// ** MAKE SURE THE HTML FILES ARE STORED AT ./web/public/ ** | |
app.get('/', (req, res, next) => { | |
res.sendFile(path.resolve(`${__dirname}/web/public/index.html`)); | |
}); | |
var server = http.createServer(app); | |
server.listen(3000, function () { | |
console.log('Example app listening on port 3000!') | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment