mirror of https://github.com/msgbyte/tailchat
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.3 KiB
TypeScript
86 lines
2.3 KiB
TypeScript
3 years ago
|
import path from 'path';
|
||
|
import express, { Router } from 'express';
|
||
|
import compression from 'compression';
|
||
|
import morgan from 'morgan';
|
||
|
import { createRequestHandler } from '@remix-run/express';
|
||
|
import raExpressMongoose from 'express-mongoose-ra-json-server';
|
||
|
import mongoose from 'mongoose';
|
||
|
import dotenv from 'dotenv';
|
||
|
dotenv.config();
|
||
|
|
||
|
// 链接数据库
|
||
|
mongoose.connect(process.env.MONGO_URL!, (error: any) => {
|
||
|
if (!error) {
|
||
|
return console.info('Mongo connected');
|
||
|
}
|
||
|
console.error(error);
|
||
|
});
|
||
|
|
||
|
const BUILD_DIR = path.join(process.cwd(), 'build');
|
||
|
|
||
|
const app = express();
|
||
|
|
||
|
app.use(compression());
|
||
|
|
||
|
// http://expressjs.com/en/advanced/best-practice-security.html#at-a-minimum-disable-x-powered-by-header
|
||
|
app.disable('x-powered-by');
|
||
|
|
||
|
// Remix fingerprints its assets so we can cache forever.
|
||
|
app.use(
|
||
|
'/build',
|
||
|
express.static('public/build', { immutable: true, maxAge: '1y' })
|
||
|
);
|
||
|
|
||
|
// Everything else (like favicon.ico) is cached for an hour. You may want to be
|
||
|
// more aggressive with this caching.
|
||
|
app.use(express.static('public', { maxAge: '1h' }));
|
||
|
|
||
|
app.use(morgan('tiny'));
|
||
|
|
||
|
const router = Router();
|
||
|
|
||
|
router.use(
|
||
|
'/users',
|
||
|
raExpressMongoose(require('../models/user/user').default, {
|
||
|
q: ['nickname', 'email'],
|
||
|
})
|
||
|
);
|
||
|
|
||
|
app.use('/admin/api', router);
|
||
|
|
||
|
app.all(
|
||
|
'/admin/*',
|
||
|
process.env.NODE_ENV === 'development'
|
||
|
? (req, res, next) => {
|
||
|
purgeRequireCache();
|
||
|
|
||
|
return createRequestHandler({
|
||
|
build: require(BUILD_DIR),
|
||
|
mode: process.env.NODE_ENV,
|
||
|
})(req, res, next);
|
||
|
}
|
||
|
: createRequestHandler({
|
||
|
build: require(BUILD_DIR),
|
||
|
mode: process.env.NODE_ENV,
|
||
|
})
|
||
|
);
|
||
|
|
||
|
const port = process.env.PORT || 3000;
|
||
|
|
||
|
app.listen(port, () => {
|
||
|
console.log(`Express server listening on port ${port}`);
|
||
|
});
|
||
|
|
||
|
function purgeRequireCache() {
|
||
|
// purge require cache on requests for "server side HMR" this won't let
|
||
|
// you have in-memory objects between requests in development,
|
||
|
// alternatively you can set up nodemon/pm2-dev to restart the server on
|
||
|
// file changes, but then you'll have to reconnect to databases/etc on each
|
||
|
// change. We prefer the DX of this, so we've included it for you by default
|
||
|
for (const key in require.cache) {
|
||
|
if (key.startsWith(BUILD_DIR)) {
|
||
|
delete require.cache[key];
|
||
|
}
|
||
|
}
|
||
|
}
|