Last active
January 12, 2019 11:20
-
-
Save pawelmadeja/d5ed083b8ce4527d6387c4270c56f0a5 to your computer and use it in GitHub Desktop.
Simple static http server with browserSync
This file contains 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 express = require('express') | |
const serveStatic = require('serve-static'); | |
const app = express(); | |
const port = process.env.PORT || 3001; | |
// Browsersync integration | |
if (app.get('env') === 'development') { | |
const browserSync = require('browser-sync'); | |
const bs = browserSync.create().init({ | |
logSnippet: false, | |
files: __dirname+'/public' | |
}); | |
app.use(require('connect-browser-sync')(bs)); | |
} | |
// Example endpoint | |
app.get('/sample', function(req, res) { | |
res.send({ hello: 'there' }) | |
}) | |
// Serve static html files in public directore | |
app.use(serveStatic(__dirname+'/public')) | |
// App listen | |
app.listen(port, () => { | |
console.log(`⚡️ Listening at http://127.0.0.1:${port}`); | |
}); |
This file contains 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
{ | |
"name": "simple-static-http-server-with-browsersync", | |
"version": "1.0.0", | |
"description": "", | |
"main": "app.js", | |
"scripts": { | |
"start": "nodemon app.js" | |
}, | |
"keywords": [], | |
"author": "pawelmadeja", | |
"license": "ISC", | |
"dependencies": { | |
"browser-sync": "^2.26.3", | |
"connect-browser-sync": "^2.1.0", | |
"express": "^4.16.4", | |
"nodemon": "^1.18.9", | |
"serve-static": "^1.13.2" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment