-
Notifications
You must be signed in to change notification settings - Fork 93
/
server.js
36 lines (26 loc) · 1.22 KB
/
server.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
const express = require( 'express' );
const bodyParser = require( 'body-parser' );
const path = require( 'path' );
const cors = require('cors');
const router = express.Router();
const app = express();
app.use( cors() );
/**
* Configure the middleware.
* bodyParser.json() returns a function that is passed as a param to app.use() as middleware
* With the help of this method, we can now send JSON to our express application.
*/
app.use( bodyParser.urlencoded( { extended: false } ) );
app.use( bodyParser.json() );
// We export the router so that the server.js file can pick it up
module.exports = router;
// Combine react and node js servers while deploying( YOU MIGHT HAVE ALREADY DONE THIS BEFORE
// What you need to do is make the build directory on the heroku, which will contain the index.html of your react app and then point the HTTP request to the client/build directory
if ( process.env.NODE_ENV === 'production' ) {
// Set a static folder
app.use( express.static( 'client/build' ) );
app.get( '*', ( req, res ) => res.sendFile( path.resolve( __dirname, 'client', 'build', 'index.html' ) ) );
}
// Set up a port
const port = process.env.PORT || 5000;
app.listen( port, () => console.log( `Server running on port: ${port}` ) );