forked from oat/in-the-database-2
62 lines
1.7 KiB
TypeScript
62 lines
1.7 KiB
TypeScript
import * as express from 'express';
|
|
import * as mongoose from 'mongoose';
|
|
import * as fs from 'fs';
|
|
import * as winston from 'winston';
|
|
|
|
import * as format from './lib/format';
|
|
|
|
const config = JSON.parse(fs.readFileSync('./config/config.json', {encoding: 'utf8'}));
|
|
|
|
const dbClient = mongoose.connect(`${config.dbconnectionURL}/${config.dbname}`, {
|
|
useNewUrlParser: true, // idfk what any of this does i just copied an example
|
|
useUnifiedTopology: true,
|
|
useFindAndModify: false,
|
|
useCreateIndex: true
|
|
});
|
|
|
|
const logger = winston.createLogger({
|
|
level: 'info',
|
|
format: winston.format.combine(
|
|
winston.format.timestamp(),
|
|
winston.format.printf(log => `${format.formatTime(new Date(log.timestamp))} | ${log.message}`)
|
|
),
|
|
transports: [
|
|
new winston.transports.File({filename: `${config.name}-error.log`, level: 'error'}),
|
|
new winston.transports.File({filename: `${config.name}.log`}),
|
|
new winston.transports.Console({
|
|
format: winston.format.combine(
|
|
winston.format.colorize(),
|
|
winston.format.timestamp(),
|
|
winston.format.printf(log =>
|
|
`${format.formatTime(new Date(log.timestamp))} - [${log.level}] ${log.message}`
|
|
)
|
|
),
|
|
level: process.env.DEBUG === 'true' ? 'silly' : 'info'
|
|
})
|
|
]
|
|
});
|
|
|
|
logger.info('connecting to mongodb database');
|
|
dbClient.then(() => {
|
|
logger.info('connected to database!');
|
|
|
|
const app = express();
|
|
|
|
app.use(express.urlencoded({ extended: true }));
|
|
|
|
app.set('db', dbClient);
|
|
app.set('config', config);
|
|
app.set('logger', logger);
|
|
|
|
app.get('/', (req, res) => {
|
|
res.send(`${config.name} homepage - unfinished`);
|
|
});
|
|
|
|
app.get('*', (req, res) => {
|
|
res.status(404).send('404');
|
|
});
|
|
|
|
app.listen(config.port, () => {
|
|
logger.info(`expressjs server launched on port ${config.port}, should now function properly`);
|
|
});
|
|
}); |