Last active
January 17, 2017 10:14
-
-
Save mateuszwrobel/0766f92d6ceeea0f9f9f42919d536b15 to your computer and use it in GitHub Desktop.
nodejs serve statics
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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
<link rel="stylesheet" href="/css/styles.css"> | |
</head> | |
<body> | |
<h1>It Works!</h1> | |
<script src="/js/bundle.js"></script> | |
</body> | |
</html> |
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
{ | |
"name": "ServeStatics", | |
"version": "1.0.0", | |
"description": "", | |
"main": "server.js", | |
"scripts": { | |
"server": "node server.js" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"body-parser": "^1.13.3", | |
"express": "^4.13.3" | |
} | |
} |
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
var express = require('express'); | |
var fs = require('fs'); | |
var bodyParser = require('body-parser'); | |
var app = express(); | |
var hostname = process.env.HOSTNAME || 'localhost'; | |
var port = parseInt(process.env.PORT, 10) || 8088; | |
app.use(express.static(__dirname + '/public')); | |
/* | |
in public folder you should have: | |
index.html | |
/css | |
/js | |
*/ | |
app.use(bodyParser.json()); | |
var server = app.listen(port, hostname, function() { | |
var _host = server.address().address; | |
var _port = server.address().port; | |
console.log('Example app listening at http://%s:%s', _host, _port); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment