mirror of
https://github.com/microsoft/TypeScript-Node-Starter.git
synced 2025-11-09 00:37:27 +00:00
replaced logging with Winston
This commit is contained in:
@@ -2,7 +2,7 @@ import express from "express";
|
||||
import compression from "compression"; // compresses requests
|
||||
import session from "express-session";
|
||||
import bodyParser from "body-parser";
|
||||
import logger from "morgan";
|
||||
import logger from "./util/logger";
|
||||
import lusca from "lusca";
|
||||
import dotenv from "dotenv";
|
||||
import mongo from "connect-mongo";
|
||||
@@ -12,7 +12,7 @@ import mongoose from "mongoose";
|
||||
import passport from "passport";
|
||||
import expressValidator from "express-validator";
|
||||
import bluebird from "bluebird";
|
||||
import { MONGODB_URI, SESSION_SECRET } from "./util/loadSecrets";
|
||||
import { MONGODB_URI, SESSION_SECRET } from "./util/secrets";
|
||||
|
||||
const MongoStore = mongo(session);
|
||||
|
||||
@@ -33,7 +33,7 @@ import * as passportConfig from "./config/passport";
|
||||
const app = express();
|
||||
|
||||
// Connect to MongoDB
|
||||
const mongoUrl = process.env.MONGOLAB_URI;
|
||||
const mongoUrl = MONGODB_URI;
|
||||
(<any>mongoose).Promise = bluebird;
|
||||
mongoose.connect(mongoUrl, {useMongoClient: true}).then(
|
||||
() => { /** ready to use. The `mongoose.connect()` promise resolves to undefined. */ },
|
||||
@@ -47,14 +47,13 @@ app.set("port", process.env.PORT || 3000);
|
||||
app.set("views", path.join(__dirname, "../views"));
|
||||
app.set("view engine", "pug");
|
||||
app.use(compression());
|
||||
app.use(logger("dev"));
|
||||
app.use(bodyParser.json());
|
||||
app.use(bodyParser.urlencoded({ extended: true }));
|
||||
app.use(expressValidator());
|
||||
app.use(session({
|
||||
resave: true,
|
||||
saveUninitialized: true,
|
||||
secret: process.env.SESSION_SECRET,
|
||||
secret: SESSION_SECRET,
|
||||
store: new MongoStore({
|
||||
url: mongoUrl,
|
||||
autoReconnect: true
|
||||
|
||||
16
src/util/logger.ts
Normal file
16
src/util/logger.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import * as winston from "winston";
|
||||
import { ENVIRONMENT } from "./secrets";
|
||||
|
||||
const logger = new (winston.Logger)({
|
||||
transports: [
|
||||
new (winston.transports.Console)({ level: process.env.NODE_ENV === "production" ? "error" : "debug" }),
|
||||
new (winston.transports.File)({ filename: "debug.log", level: "debug"})
|
||||
]
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
logger.debug("Logging initialized at debug level");
|
||||
}
|
||||
|
||||
export default logger;
|
||||
|
||||
26
src/util/secrets.ts
Normal file
26
src/util/secrets.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import logger from "./logger";
|
||||
import * as dotenv from "dotenv";
|
||||
import * as fs from "fs";
|
||||
|
||||
if (fs.existsSync(".env")) {
|
||||
logger.debug("Using .env file to supply config environment variables");
|
||||
dotenv.config({ path: ".env" });
|
||||
} else {
|
||||
logger.debug("Using .env.example file to supply config environment variables");
|
||||
dotenv.config({ path: ".env.example" }); // you can delete this after you create your own .env file!
|
||||
}
|
||||
export const ENVIRONMENT = process.env.NODE_ENV;
|
||||
const prod = ENVIRONMENT === "production"; // Anything else is treated as 'dev'
|
||||
|
||||
export const SESSION_SECRET = process.env["SESSION_SECRET"];
|
||||
export const MONGODB_URI = prod ? process.env["MONGODB_URI"] : process.env["MONGODB_URI_LOCAL"];
|
||||
|
||||
if (!SESSION_SECRET) {
|
||||
logger.error("No client secret. Set SESSION_SECRET environment variable.");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
if (!MONGODB_URI) {
|
||||
logger.error("No mongo connection string. Set MONGODB_URI environment variable.");
|
||||
process.exit(1);
|
||||
}
|
||||
Reference in New Issue
Block a user