Skip to content

Commit e1427ec

Browse files
committed
Initial commit
0 parents  commit e1427ec

File tree

12 files changed

+183
-0
lines changed

12 files changed

+183
-0
lines changed

.gitignore

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
lib-cov
2+
*.seed
3+
*.log
4+
*.csv
5+
*.dat
6+
*.out
7+
*.pid
8+
*.gz
9+
10+
pids
11+
logs
12+
results
13+
_dev
14+
bower_components
15+
16+
npm-debug.log
17+
node_modules
18+
server_old
19+
public

.jshintrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"browser" : true,
3+
"esnext" : true
4+
}

Procfile

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web: npm start

index.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require("babel/register");
2+
require('./server');

package.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"name": "express-es6-rest-api",
3+
"version": "0.1.0",
4+
"description": "Starter project for an ES6 RESTful Express API",
5+
"main": "index.js",
6+
"scripts": {
7+
"start": "node index.js",
8+
"test": "echo \"Error: no test specified\" && exit 1"
9+
},
10+
"author": "Jason Miller <[email protected]>",
11+
"license": "Proprietary",
12+
"dependencies": {
13+
"babel": "^4.7.16",
14+
"body-parser": "^1.12.2",
15+
"compression": "^1.4.3",
16+
"cors": "^2.5.3",
17+
"express": "^4.12.3",
18+
"resource-router-middleware": "^0.4.0"
19+
}
20+
}

server/api/facets.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import resource from 'resource-router-middleware';
2+
import facets from '../models/facets';
3+
4+
export default resource({
5+
6+
/** Property name to store preloaded entity on `request`. */
7+
id : 'facet',
8+
9+
/** For requests with an `id`, you can auto-load the entity.
10+
* Errors terminate the request, success sets `req[id] = data`.
11+
*/
12+
load(req, id, callback) {
13+
var facet = facets.find( facet => facet.id===id ),
14+
err = facet ? null : 'Not found';
15+
callback(err, user);
16+
},
17+
18+
/** GET / - List all entities */
19+
index({ params }, res) {
20+
res.json(facets);
21+
},
22+
23+
/** POST / - Create a new entity */
24+
create({ body }, res) {
25+
body.id = facets.length.toString(36);
26+
facets.push(body);
27+
res.json(body);
28+
},
29+
30+
/** GET /:id - Return a given entity */
31+
read({ params }, res) {
32+
res.json(req.facet);
33+
},
34+
35+
/** PUT /:id - Update a given entity */
36+
update({ facet, body }, res) {
37+
for (let key in body) {
38+
if (key!=='id') {
39+
facet[key] = body[key];
40+
}
41+
}
42+
res.sendStatus(204);
43+
},
44+
45+
/** DELETE /:id - Delete a given entity */
46+
delete({ facet }, res) {
47+
facets.splice(facets.indexOf(facet), 1);
48+
res.sendStatus(204);
49+
}
50+
});

server/api/index.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Router } from 'express';
2+
import facets from './facets';
3+
4+
export default function() {
5+
var api = Router();
6+
7+
// mount the facets resource
8+
api.use('/facets', facets);
9+
10+
// perhaps expose some API metadata at the root
11+
api.get('/', (req, res) => {
12+
res.json({
13+
version : '1.0'
14+
});
15+
});
16+
17+
return api;
18+
}

server/db.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default function(callback) {
2+
// connect to the database
3+
callback();
4+
}

server/index.js

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import http from 'http';
2+
import express from 'express';
3+
import cors from 'cors';
4+
import bodyParser from 'body-parser';
5+
import db from './db';
6+
import middleware from './middleware';
7+
import api from './api';
8+
9+
var app = express();
10+
app.server = http.createServer(app);
11+
12+
// 3rd party middleware
13+
app.use(cors());
14+
15+
app.use(bodyParser.json({
16+
limit : '100kb'
17+
}));
18+
19+
// connect to db
20+
db( λ => {
21+
22+
// internal middleware
23+
app.use(middleware());
24+
25+
// api router
26+
app.use('/api', api());
27+
28+
app.server.listen(process.env.PORT || 8080);
29+
30+
console.log(`Started on port ${app.server.address().port}`);
31+
});
32+
33+
export default app;

server/lib/util.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
/** Creates a callback that proxies node callback style arguments to an Express Response object.
3+
* @param {express.Response} res Express HTTP Response
4+
* @param {number} [status=200] Status code to send on success
5+
*
6+
* @example
7+
* list(req, res) {
8+
* collection.find({}, toRes(res));
9+
* }
10+
*/
11+
export function toRes(res, status=200) {
12+
return (err, thing) => {
13+
if (err) return res.status(500).send(err);
14+
15+
if (thing && typeof thing.toObject==='function') {
16+
thing = thing.toObject();
17+
}
18+
res.status(status).json(thing);
19+
};
20+
}

server/middleware/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { Router } from 'express';
2+
3+
export default function() {
4+
var routes = Router();
5+
6+
// add middleware here
7+
8+
return routes;
9+
}

server/models/facets.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// our example model is just an Array
2+
var facets = [];
3+
export default facets;

0 commit comments

Comments
 (0)