Skip to content

Commit 061e978

Browse files
committed
Sets up babel and express server
1 parent 6e97049 commit 061e978

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

.babelrc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"presets": ["react", "es2015", "stage-0"],
3+
"plugins": [
4+
["transform-decorators-legacy"],
5+
["add-module-exports"],
6+
]
7+
}

package.json

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Univeral JS application utilizing Hot Module Reloading, Server Side Rendering, Code Splitting with React, Redux, React Router 4 and a heroku like deploy setup with Dokku.",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1"
7+
"development": "NODE_PATH=./src NODE_ENV=development node ./src/server/server.babel.js",
8+
"production": "NODE_PATH=./src NODE_ENV=production node ./src/server/server.babel.js"
89
},
910
"keywords": [
1011
"react",
@@ -17,5 +18,17 @@
1718
"webpack"
1819
],
1920
"author": "Alexander J Ray <[email protected]>",
20-
"license": "MIT"
21+
"license": "MIT",
22+
"devDependencies": {
23+
"babel-plugin-add-module-exports": "^0.2.1",
24+
"babel-plugin-transform-decorators-legacy": "^1.3.4",
25+
"babel-preset-es2015": "^6.24.0",
26+
"babel-preset-react": "^6.23.0",
27+
"babel-preset-stage-0": "^6.22.0",
28+
"babel-register": "^6.24.0"
29+
},
30+
"dependencies": {
31+
"colors": "^1.1.2",
32+
"express": "^4.15.2"
33+
}
2134
}

src/server/server.babel.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// All subsequent files required by node with the extensions .es6, .es, .jsx and .js will be transformed by Babel.
2+
require('babel-register');
3+
4+
// Server Driver Code, everything from here on can use all the super future ES6 features!
5+
module.exports = require('./server.js');

src/server/server.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import http from 'http';
2+
import express from 'express';
3+
import colors from 'colors';
4+
import path from 'path';
5+
6+
const PROD = process.env.NODE_ENV === 'production';
7+
8+
const app = express();
9+
10+
// catch 404 and forward to error handler
11+
app.use(function(req, res, next) {
12+
var err = new Error('Not Found');
13+
err.status = 404;
14+
next(err);
15+
});
16+
17+
// development error handler
18+
if (!PROD) {
19+
app.use(function(err, req, res, next) {
20+
console.error('error : ', err)
21+
res.status(err.status || 500);
22+
});
23+
}
24+
25+
// production error handler
26+
app.use(function(err, req, res, next) {
27+
console.error('error : ', err.message)
28+
res.status(err.status || 500);
29+
});
30+
31+
const server = http.createServer(app);
32+
33+
server.listen(8080, function() {
34+
const address = server.address();
35+
console.log(`${'>>>'.cyan} ${'Listening on:'.rainbow} ${'localhost::'.trap.magenta}${`${address.port}`.green}`);
36+
});

0 commit comments

Comments
 (0)