Last active
April 5, 2016 09:35
-
-
Save roughy/c2e7cbf4f818cef466df87d81b57829d to your computer and use it in GitHub Desktop.
Start a simple node Http server for testing puposes.
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
/** | |
* Installation: npm install express path | |
* | |
* Create file json/users.json | |
* [ | |
* { | |
* "name": "Marco", | |
* "description": "Test" | |
* },{ | |
* "name": "Another", | |
* "description": "User" | |
* } | |
* ] | |
* | |
* Run with: $> node JsonHttpServer.js 8081 | |
* | |
* Links: | |
* - http://expressjs.com/en/4x/api.html | |
*/ | |
var express = require('express'), | |
path = require('path'), | |
app = express(), | |
port = process.argv[2] || 8080; | |
app.get('/api/users', function (req, res) { | |
res.set('Access-Control-Allow-Origin', '*'); | |
res.type('json'); | |
res.sendFile(path.join(__dirname, 'json/users.json')); | |
}); | |
app.get('/api/users/marco', function (req, res) { | |
const usersFile = path.join(__dirname, 'json/users.json'); | |
const users = require(usersFile); | |
res.set('Access-Control-Allow-Origin', '*'); | |
res.type('json'); | |
res.send(users.find(user => user.name === "Marco")); | |
}); | |
app.listen(port, function() { | |
console.log('Listening on port %d', port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment