Project Structure
public/
css/
js/
index.html
.gitignore
app.js
// add to `app.js`
// reference: http://expressjs.com/starter/static-files.html
app.use(express.static('public'));| //app.js | |
| var config = require('./config.json'); | |
| var express = require('express'); | |
| var app = express(); | |
| app.use(express.static('public')); | |
| app.get('/', function (request, response) { | |
| response.send('Hello, World!'); | |
| }); | |
| app.get('/greet/:name', function (request, response) { | |
| var name = request.params.name; | |
| response.json({ message: "Hello, " + name }); | |
| }); | |
| var users = []; | |
| app.get('/create/users/:username', function (req, res) { | |
| var username = req.params.username; | |
| if (users.indexOf(username) < 0) { | |
| users.push(username); | |
| } | |
| var index = users.indexOf(username); | |
| res.json({id: index, username: username}); | |
| }) | |
| app.get('/users', function(req, res) { | |
| res.json(users); | |
| }) | |
| app.get('/add/:x/:y', function (request, response) { | |
| var x = request.params.x; | |
| var y = request.params.y; | |
| response.json({ answer: x + y }); | |
| }) | |
| var server = app.listen(config.port, displayServerInfo); | |
| function displayServerInfo () { | |
| var host = server.address().address; | |
| var port = server.address().port; | |
| console.log('Listening at http://%s:%s', host, port); | |
| } |
Project Structure
public/
css/
js/
index.html
.gitignore
app.js
// add to `app.js`
// reference: http://expressjs.com/starter/static-files.html
app.use(express.static('public'));