app access for dev

This commit is contained in:
Владислав Чикалкин 2020-09-24 11:04:45 +03:00
parent 77236142d9
commit 071fb891d3
3 changed files with 27 additions and 23 deletions

View File

@ -51,6 +51,7 @@
"@types/cors": "^2.8.7", "@types/cors": "^2.8.7",
"@types/express": "^4.17.7", "@types/express": "^4.17.7",
"@types/faker": "^5.1.0", "@types/faker": "^5.1.0",
"@types/ip": "^1.1.0",
"@types/lodash": "^4.14.159", "@types/lodash": "^4.14.159",
"@types/morgan": "^1.9.1", "@types/morgan": "^1.9.1",
"@types/node": "^14.6.0", "@types/node": "^14.6.0",
@ -61,6 +62,7 @@
"dotenv": "^8.2.0", "dotenv": "^8.2.0",
"faker": "^5.1.0", "faker": "^5.1.0",
"http-proxy-middleware": "^1.0.5", "http-proxy-middleware": "^1.0.5",
"ip": "^1.1.5",
"rimraf": "^3.0.2", "rimraf": "^3.0.2",
"webpack-node-externals": "^2.5.1" "webpack-node-externals": "^2.5.1"
}, },

View File

@ -1,4 +1,5 @@
import { address } from 'ip';
export const API_PORT = 3001; export const API_PORT = 3001;
export const API_URL = "http://localhost:" + API_PORT; export const API_URL = `http://${address()}:${API_PORT}`;
export const CLIENT_DEV_URL = "http://localhost:3000"; export const CLIENT_DEV_URL = 'http://localhost:3000';

View File

@ -1,21 +1,21 @@
import bodyParser from "body-parser"; import bodyParser from 'body-parser';
import compression from "compression"; import compression from 'compression';
import cors from "cors"; import cors from 'cors';
// import cookieParser from "cookie-parser"; // import cookieParser from "cookie-parser";
import express from "express"; import express from 'express';
// import nodeSSPI from "express-node-sspi"; // import nodeSSPI from "express-node-sspi";
import helmet from "helmet"; import helmet from 'helmet';
import morgan from "morgan"; import morgan from 'morgan';
import path from "path"; import path from 'path';
import "reflect-metadata"; import 'reflect-metadata';
import routes from "./routes"; import routes from './routes';
import { CLIENT_DEV_URL, API_PORT } from "../core/constants/urls"; import { CLIENT_DEV_URL, API_PORT } from '../core/constants/urls';
const isDevelopmentMode = process.env.NODE_ENV === "development"; const isDevelopmentMode = process.env.NODE_ENV === 'development';
const buildDir = path.join( const buildDir = path.join(
process.cwd(), process.cwd(),
process.env.NODE_ENV === "development" ? "/build" : "" process.env.NODE_ENV === 'development' ? '/build' : '',
); );
const app = express(); const app = express();
@ -37,30 +37,31 @@ app.use(bodyParser.json());
app.use( app.use(
bodyParser.urlencoded({ bodyParser.urlencoded({
extended: true, extended: true,
}) }),
); );
app.use(helmet({ contentSecurityPolicy: { reportOnly: true } })); app.use(helmet({ contentSecurityPolicy: { reportOnly: true } }));
if (isDevelopmentMode) { app.use(
app.use(cors({ origin: CLIENT_DEV_URL, credentials: true })); cors({ origin: isDevelopmentMode ? '*' : CLIENT_DEV_URL, credentials: true }),
} );
// app.use(cookieParser()); // app.use(cookieParser());
app.use(compression()); app.use(compression());
app.use(morgan("tiny")); app.use(morgan(isDevelopmentMode ? 'dev' : 'tiny'));
/**EXTENTIONS */ /**EXTENTIONS */
/**ROUTES */ /**ROUTES */
app.use("/", routes); app.use('/', routes);
/**ROUTES */ /**ROUTES */
/**CLIENT */ /**CLIENT */
app.use(express.static(buildDir)); app.use(express.static(buildDir));
app.get("*", function (req, res) { app.get('*', function (req, res) {
res.sendFile(path.join(buildDir, "index.html")); res.sendFile(path.join(buildDir, 'index.html'));
}); });
/**CLIENT */ /**CLIENT */
console.log("checking port", API_PORT); console.log('checking port', API_PORT);
app.listen(API_PORT, () => { app.listen(API_PORT, () => {
console.log(`Server now listening on port: ${API_PORT}`); console.log(`Server now listening on port: ${API_PORT}`);
}); });