Created
July 8, 2013 16:09
-
-
Save alexito4/5950169 to your computer and use it in GitHub Desktop.
Basic REST API with NodeJS and Express
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
var express = require('express'); | |
var app = express(); | |
app.use(express.bodyParser()); | |
var users = [ | |
{ | |
"id": 0, | |
"isActive": false, | |
"balance": "$1,274.00", | |
"picture": "http://lorempixel.com/50/50", | |
"age": 25, | |
"name": "Campbell Solomon", | |
"gender": "male", | |
"company": "Autograte", | |
"email": "[email protected]", | |
"phone": "+1 (812) 456-2742", | |
"address": "197 Truxton Street, Why, New Jersey, 1050", | |
"about": "Eiusmod commodo ut incididunt cillum est nostrud anim ullamco esse commodo. Ut magna et tempor minim dolor sunt adipisicing elit. Minim pariatur et ad et laborum laborum elit. Irure anim ut esse qui ipsum dolore velit anim est consectetur. Laborum dolor commodo ea laboris eiusmod. Incididunt labore aliqua culpa adipisicing esse anim exercitation sint est nisi. Do excepteur incididunt voluptate commodo consectetur labore anim cupidatat tempor sunt adipisicing commodo nostrud.\r\n" | |
}, | |
{ | |
"id": 1, | |
"isActive": true, | |
"balance": "$2,223.00", | |
"picture": "http://lorempixel.com/50/50", | |
"age": 35, | |
"name": "West Foreman", | |
"gender": "male", | |
"company": "Imant", | |
"email": "[email protected]", | |
"phone": "+1 (824) 495-2270", | |
"address": "795 Columbia Place, Taft, Connecticut, 8168", | |
"about": "Quis nostrud et laborum sit eu aute enim. Exercitation dolore Lorem incididunt aliquip ex. Pariatur laborum veniam ipsum veniam ad cillum duis consequat sit adipisicing aliqua officia quis duis. Do pariatur voluptate duis pariatur ea ea.\r\n" | |
}, | |
{ | |
"id": 2, | |
"isActive": true, | |
"balance": "$2,450.00", | |
"picture": "http://lorempixel.com/50/50", | |
"age": 37, | |
"name": "Whitney Jackson", | |
"gender": "male", | |
"company": "Flotonic", | |
"email": "[email protected]", | |
"phone": "+1 (997) 571-3640", | |
"address": "328 Willow Place, Sunwest, Idaho, 1921", | |
"about": "Cupidatat fugiat do deserunt pariatur aliquip adipisicing nulla occaecat eiusmod elit laboris nisi. Enim magna sunt duis sit nisi esse minim eu. Labore nisi veniam cupidatat ea ipsum duis anim quis. Sit minim veniam non eiusmod elit quis incididunt pariatur sint culpa amet.\r\n" | |
}, | |
{ | |
"id": 3, | |
"isActive": false, | |
"balance": "$3,771.00", | |
"picture": "http://lorempixel.com/50/50", | |
"age": 39, | |
"name": "Dotson Glover", | |
"gender": "male", | |
"company": "Ovolo", | |
"email": "[email protected]", | |
"phone": "+1 (962) 493-2329", | |
"address": "213 Stone Avenue, Faywood, Alaska, 1105", | |
"about": "Esse id reprehenderit eu non amet sunt laborum incididunt nisi enim. Nulla officia ullamco pariatur laboris ex aute sint culpa nulla eiusmod velit labore. Voluptate anim incididunt labore laborum. Do veniam excepteur ex adipisicing est tempor consectetur duis esse mollit laboris ut.\r\n" | |
}, | |
{ | |
"id": 4, | |
"isActive": true, | |
"balance": "$2,115.00", | |
"picture": "http://lorempixel.com/50/50", | |
"age": 35, | |
"name": "Pugh Adkins", | |
"gender": "male", | |
"company": "Isis", | |
"email": "[email protected]", | |
"phone": "+1 (904) 536-2404", | |
"address": "308 Kent Avenue, Walton, Vermont, 7366", | |
"about": "Laborum laboris consectetur esse Lorem sit velit nulla tempor aliquip nisi laboris deserunt minim. Dolore commodo id velit sint enim tempor dolore. Pariatur ea tempor culpa in irure laborum. Do Lorem voluptate incididunt esse enim. Do officia eu proident laboris. Dolore elit officia dolore sunt. Ad enim cupidatat velit quis irure nisi.\r\n" | |
} | |
]; | |
app.get('/users', function(req, res) { | |
res.json(users); | |
}); | |
app.get('/users/:id', function(req, res) { | |
var id = req.params.id - 1; | |
if(users.length <= id || id < 0) { | |
res.statusCode = 404; | |
return res.send('Error 404: No user found'); | |
} | |
var u = users[id]; | |
res.json(u); | |
}); | |
app.listen(process.env.PORT || 3000); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment