Skip to content

Instantly share code, notes, and snippets.

@sgnl
Last active August 29, 2015 14:22
Show Gist options
  • Save sgnl/f69d5c70291596eba9c0 to your computer and use it in GitHub Desktop.
Save sgnl/f69d5c70291596eba9c0 to your computer and use it in GitHub Desktop.
//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'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment