-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
63 lines (50 loc) · 1.6 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
'use strict';
const NODE_ENV = process.env.NODE_ENV || 'development';
const path = require('path');
const util = require('util');
const fs = require('fs');
const connect = require( 'connect' );
const connectRoute = require('connect-route');
const serveStatic = require('serve-static');
const http = require( 'http' );
// const React = require('react');
// const ReactDOMServer = require('react-dom/server');
const app = connect();
// gzip/deflate outgoing responses
const compression = require('compression');
app.use( compression() );
app.use(serveStatic(path.join(__dirname, 'public')));
// parse urlencoded request bodies into req.body
const bodyParser = require( 'body-parser' );
app.use(bodyParser.json());
app.use( bodyParser.urlencoded( {
extended: false
} ) );
// respond to all requests
app.use( function( req, res, next ) {
next();
} );
app.use(connectRoute(router => {
/* GET / */
router.get('/', (req, res, next) => {
fs.readFile(path.resolve(__dirname, 'views/index.html'), 'utf8', (err, data) => {
if(err) {
return;
}
res.setHeader('Content-Type', 'text/html');
res.end(data);
})
});
/* GET /sdk.js */
router.get('/sdk.js', (req, res, next) => {
fs.readFile(path.resolve(__dirname, 'public/js/sdk.js'), 'utf8', (err, data) => {
if(err) {
return;
}
res.setHeader('Content-Type', 'application/javascript');
res.end(data);
})
});
}))
//create node.js http server and listen on port
http.createServer( app ).listen( 3000 );