Skip to content

Commit 15c29f1

Browse files
committed
Switch to eslint, bundle nodemon for development live-reload (npm run dev), pre-transpile for production, add configuration, make everything instantiable instead of singletons.
1 parent 27acd43 commit 15c29f1

16 files changed

+63
-67
lines changed

.babelrc

-3
This file was deleted.

.gitignore

+4-22
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,5 @@
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
20-
21-
# OS generated stuff
1+
/dist
2+
/logs
3+
/npm-debug.log
4+
/node_modules
225
.DS_Store
23-
.DS_Store?

.jshintrc

-4
This file was deleted.

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2015 Jason Miller
3+
Copyright (c) 2016 Jason Miller
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy of
66
this software and associated documentation files (the "Software"), to deal in

README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ rm -rf .git && git init && npm init
2626
# Install dependencies
2727
npm install
2828

29-
# Run it
30-
PORT=8080 npm start
29+
# Start development live-reload server
30+
PORT=8080 npm run dev
3131

32-
# With nodemon:
33-
PORT=8080 nodemon
32+
# Start production server:
33+
PORT=8080 npm start
3434
```
3535

3636

index.js

-1
This file was deleted.

package.json

+26-9
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,32 @@
11
{
22
"name": "express-es6-rest-api",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"description": "Starter project for an ES6 RESTful Express API",
5-
"main": "index.js",
5+
"main": "dist",
66
"scripts": {
7-
"start": "babel-node index.js",
8-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"dev": "nodemon -w src --exec 'babel-node src --presets es2015,stage-0'",
8+
"build": "babel src -s -D -d dist --presets es2015,stage-0",
9+
"start": "node dist",
10+
"prestart": "npm run -s build",
11+
"test": "eslint src"
912
},
10-
"repository": {
11-
"type": "git",
12-
"url": "git://github.com/developit/express-es6-rest-api.git"
13+
"eslintConfig": {
14+
"extends": "eslint:recommended",
15+
"parserOptions": {
16+
"ecmaVersion": 7,
17+
"sourceType": "module"
18+
},
19+
"env": {
20+
"node": true
21+
},
22+
"rules": {
23+
"no-console": 0,
24+
"no-unused-vars": 1
25+
}
1326
},
27+
"repository": "developit/express-es6-rest-api",
1428
"author": "Jason Miller <[email protected]>",
15-
"license": "Proprietary",
29+
"license": "MIT",
1630
"dependencies": {
1731
"body-parser": "^1.13.3",
1832
"compression": "^1.5.2",
@@ -23,6 +37,9 @@
2337
"devDependencies": {
2438
"babel-cli": "^6.9.0",
2539
"babel-core": "^6.9.0",
26-
"babel-preset-es2015": "^6.9.0"
40+
"babel-preset-es2015": "^6.9.0",
41+
"babel-preset-stage-0": "^6.5.0",
42+
"eslint": "^3.1.1",
43+
"nodemon": "^1.9.2"
2744
}
2845
}

server/db.js

-4
This file was deleted.

server/api/facets.js src/api/facets.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import resource from 'resource-router-middleware';
22
import facets from '../models/facets';
33

4-
export default resource({
4+
export default ({ config, db }) => resource({
55

66
/** Property name to store preloaded entity on `request`. */
77
id : 'facet',
@@ -10,7 +10,7 @@ export default resource({
1010
* Errors terminate the request, success sets `req[id] = data`.
1111
*/
1212
load(req, id, callback) {
13-
var facet = facets.find( facet => facet.id===id ),
13+
let facet = facets.find( facet => facet.id===id ),
1414
err = facet ? null : 'Not found';
1515
callback(err, facet);
1616
},

server/api/index.js src/api/index.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1+
import { version } from '../../package.json';
12
import { Router } from 'express';
23
import facets from './facets';
34

4-
export default function() {
5-
var api = Router();
5+
export default ({ config, db }) => {
6+
let api = Router();
67

78
// mount the facets resource
8-
api.use('/facets', facets);
9+
api.use('/facets', facets({ config, db }));
910

1011
// perhaps expose some API metadata at the root
1112
api.get('/', (req, res) => {
12-
res.json({
13-
version : '1.0'
14-
});
13+
res.json({ version });
1514
});
1615

1716
return api;

src/config.json

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"port": 8080,
3+
"bodyLimit": "100kb",
4+
"corsHeaders": ["Link"]
5+
}

src/db.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default callback => {
2+
// connect to a database if needed, the pass it to `callback`:
3+
callback();
4+
}

server/index.js src/index.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,33 @@ import http from 'http';
22
import express from 'express';
33
import cors from 'cors';
44
import bodyParser from 'body-parser';
5-
import db from './db';
5+
import initializeDb from './db';
66
import middleware from './middleware';
77
import api from './api';
8+
import config from './config.json';
89

9-
var app = express();
10+
let app = express();
1011
app.server = http.createServer(app);
1112

1213
// 3rd party middleware
1314
app.use(cors({
14-
exposedHeaders: ['Link']
15+
exposedHeaders: config.corsHeaders
1516
}));
1617

1718
app.use(bodyParser.json({
18-
limit : '100kb'
19+
limit : config.bodyLimit
1920
}));
2021

2122
// connect to db
22-
db( λ => {
23+
initializeDb( db => {
2324

2425
// internal middleware
25-
app.use(middleware());
26+
app.use(middleware({ config, db }));
2627

2728
// api router
28-
app.use('/api', api());
29+
app.use('/api', api({ config, db }));
2930

30-
app.server.listen(process.env.PORT || 8080);
31+
app.server.listen(process.env.PORT || config.port);
3132

3233
console.log(`Started on port ${app.server.address().port}`);
3334
});

server/lib/util.js src/lib/util.js

File renamed without changes.

server/middleware/index.js src/middleware/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Router } from 'express';
22

3-
export default function() {
4-
var routes = Router();
3+
export default ({ config, db }) => {
4+
let routes = Router();
55

66
// add middleware here
77

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

0 commit comments

Comments
 (0)