Created
April 4, 2019 22:23
-
-
Save ivanleoncz/a77355a24e23f0f8559a2676c3fe4210 to your computer and use it in GitHub Desktop.
Simple Express.js app, following REST Architecture, with tests included (Jest + Supertest).
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
const express = require('express'); | |
const bodyParser = require('body-parser'); | |
var app = express(); | |
var router = express.Router(); | |
app.use(bodyParser.json()); | |
app.use(bodyParser.urlencoded({extended: true})); | |
var tasks = [ | |
{id: '1', title: "Create REST API with ExpressJS", user:"ivanleonczBR"}, | |
{id: '2', title: "Build Front-end in AngularJS", user:"erikaUS"}, | |
{id: '3', title: "Integrate Mobile App with REST API", user:"emiliaDE"}, | |
{id: '4', title: "Design Tests for Mobile App", user:"charlesUK"} | |
]; | |
router.get('/tasks', function(req, res) { | |
res.send(tasks); | |
}); | |
router.get('/tasks/:task_id', function(req, res) { | |
// tasks start in 0, not 1, for Array context | |
res.send(tasks[req.params.task_id - 1]); | |
}); | |
router.post('/tasks/', function(req, res) { | |
// tasks start in 0, not 1, for Array context | |
// getting last Array record for generating an id for the new task | |
const idx = parseInt(tasks[tasks.length - 1].id) + 1; | |
const title = req.body.title; | |
const user = req.body.user; | |
tasks.push({id: idx.toString(), title:title, user:user}); | |
res.send("Done!"); | |
}); | |
router.put('/tasks/:task_id', function(req, res) { | |
// tasks start in 0, not 1, for Array context | |
const idx = req.params.task_id - 1; | |
const title = req.body.title; | |
const user = req.body.user; | |
tasks[idx].title = title; | |
tasks[idx].user = user; | |
res.send("Done!"); | |
}); | |
router.delete('/tasks/:task_id', function(req, res) { | |
// tasks start in 0, not 1, for Array context | |
tasks.pop(parseInt(req.params.task_id) -1) | |
res.send("Done!"); | |
}); | |
app.use('/api/v1.0', router); | |
module.exports = app; |
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
const assert = require('assert'); | |
const request = require('supertest'); | |
const app = require('./app') | |
describe('POST: create new task (id:5)', function() { | |
test('It should return response 200.', (done) => { | |
request(app).post('/api/v1.0/tasks').send({ | |
title: "Code Refactor", | |
user: "ivanleonczBR" | |
}).expect(200).end(function(err, res) { | |
done(); | |
}); | |
}); | |
}); | |
describe('GET: created task (id:5)', function() { | |
test('It respond with json', function() { | |
return request(app) | |
.get('/api/v1.0/tasks/5') | |
.set('Accept', 'application/json') | |
.expect('Content-Type', /json/) | |
.expect(200) | |
.then(response => { | |
assert(response.body.title, 'Code Refactor'); | |
}) | |
}); | |
}); | |
describe('PUT: update task (id:5)', function() { | |
test('It should return response 200.', function() { | |
return request(app) | |
.put('/api/v1.0/tasks/5') | |
.send({title:'Code Refactor API',user:'ivanleoncz'}) | |
.expect(200); | |
}); | |
}); | |
describe('DELETE: delete task (id:5)', function() { | |
test('It should return response 200', function() { | |
return request(app) | |
.del('/api/v1.0/tasks/5') | |
.expect(200); | |
}); | |
}); | |
describe('GET: all tasks', function() { | |
test('It should response 200.', (done) => { | |
request(app).get('/api/v1.0/tasks').then(function(response) { | |
expect(200); | |
done(); | |
}); | |
}); | |
}); |
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
{ | |
"name": "simple_rest", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "jest --coverage", | |
"start": "node server.js" | |
}, | |
"author": "ivanleoncz", | |
"license": "GPL-3.0", | |
"dependencies": { | |
"body-parser": "^1.18.3", | |
"ejs": "^2.6.1", | |
"express": "^4.16.4" | |
}, | |
"devDependencies": { | |
"jest": "^24.5.0", | |
"supertest": "^4.0.2" | |
} | |
} |
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
const app = require('./app'); | |
const port = 3000; | |
app.listen(port, function() { | |
console.log("Server is running: http://localhost:%s", port); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment