43 lines
972 B
JavaScript
43 lines
972 B
JavaScript
'use strict';
|
|
|
|
require('dotenv').config();
|
|
|
|
const express = require("express");
|
|
const basicAuth = require('express-basic-auth');
|
|
const path = require('path');
|
|
const routes = require('./routes');
|
|
|
|
const { myAuthorizer, getUnauthorizedResponse } = require('./helpers/auth');
|
|
|
|
const app = express();
|
|
const port = process.env.PORT || 5000;
|
|
|
|
|
|
app.use(express.json());
|
|
|
|
app.use('/api', routes);
|
|
|
|
app.use(basicAuth({
|
|
authorizer: myAuthorizer,
|
|
challenge: true,
|
|
unauthorizedResponse: getUnauthorizedResponse
|
|
}));
|
|
|
|
//Static file declaration
|
|
app.use(express.static(path.join(__dirname, 'client/build')));
|
|
|
|
//production mode
|
|
if(process.env.NODE_ENV === 'production') {
|
|
app.get('*', (req, res) => {
|
|
res.sendfile(path.join(__dirname = 'client/build/index.html'));
|
|
});
|
|
}
|
|
|
|
//build mode
|
|
app.get('*', (req, res) => {
|
|
res.sendFile(path.join(__dirname + '/client/public/index.html'));
|
|
});
|
|
|
|
|
|
app.listen(port, () => console.log(`App running on port ${port}!`));
|