我在运行节点/快速服务器时遇到问题。 服务器运行得很好,但随着时间的推移会占用大量内存。 它的内存使用量约为70mb。但是每次请求需要大约2mb。 很快就达到1.5GB的标记并冻结。它也不会随着时间的推移释放任何内存。 我的server.js中是否有任何常规错误:
import 'babel/polyfill';
import _ from 'lodash';
import fs from 'fs';
import path from 'path';
import express from 'express';
import ReactDOM from 'react-dom/server';
import router from './router';
import Translator from './core/Translator.js';
import cookieParser from 'cookie-parser';
import StoreContainer from './utils/redux.js';
import { combineReducers } from 'redux';
import reducers from './reducers';
import {setAuthToken, removeAuthToken} from './utils/api';
import i18nLocation from './utils/i18nLocation.js';
import {api, setLangHeader, setProtocol} from './utils/api';
import {clearActiveApartment} from './actions/activeApartment.js';
import {clearCityInfo} from './actions/city.js';
import {setDevice} from './actions/appData.js';
import MobileDetect from 'mobile-detect';
import StaticConfiguration from './constants/StaticConfiguration.js';
import compression from 'compression';
const server = global.server = express();
server.set('port', (process.env.PORT || 5000));
server.use(cookieParser());
server.use(compression());
server.use(express.static(path.join(__dirname, 'public')));
// The top-level React component + HTML template for it
const templateFile = path.join(__dirname, 'templates/index.html');
const template = _.template(fs.readFileSync(templateFile, 'utf8'));
server.get(/^[^.]+$/, async (req, res, next) => {
setProtocol(req.protocol);
StoreContainer.emptyStore();
let store = StoreContainer.store;
let data = _.pick(req, ['baseUrl', 'ip', 'hostname', 'originalUrl',
'path', 'protocol', 'headers']);
api.saveRequest({
data: data
}).then(response => {
}, _.noop);
try {
let statusCode = 200;
let data = { title: '', description: '', css: '', body: '',
country: '', city: '', lonlat: '', image: '/images/test.jpg', fbId: StaticConfiguration.fbKey,
analytics: StaticConfiguration.analyticsKey};
const css = [];
let lang = Translator.resolveLangForRequest(req);
lang = 'de'; // TODO: remove this line to allow other langs
Translator.initialize({descriptor: lang});
setLangHeader(lang);
if (req.cookies && req.cookies['wg-token']) {
setAuthToken(req.cookies['wg-token']);
}
const context = {
onInsertCss: value => css.push(value),
onSetTitle: value => data.title = value,
onSetMeta: (key, value) => data[key] = value,
onPageNotFound: () => statusCode = 404
};
await router.dispatch({ path: req.path, context }, (state, component) => {
// redirect to another page
if (state.redirect) {
res.redirect(state.redirect);
}
data.body = ReactDOM.renderToString(component);
data.css = css.join('');
data.langFile = Translator.resolveLangFile(lang, req);
data.initialState = JSON.stringify(store.getState());
data.langCode = lang;
});
removeAuthToken();
let html = template(data);
res.status(statusCode).send(html);
} catch (err) {
next(err);
}
});
//
// Launch the server
// -----------------------------------------------------------------------------
server.listen(server.get('port'), () => {
if (process.send) {
process.send('online');
} else {
console.log('The server is running at http://localhost:' + server.get('port'));
}
});
答案 0 :(得分:1)
res.direct
可能存在问题。它应该被返回,因此调度函数不会继续运行data.body =
,data.css =
等。检查请求是否已被重定向也可能有所帮助,以便调用后的代码如果您已经重定向,则await router.dispatch()
无法运行。
例如:
let redirected = false
await router.dispatch({ path: req.path, context }, (state, component) => {
// redirect to another page
if (state.redirect) {
redirected = true;
res.redirect(state.redirect);
return; // stop data.body, data.css, etc from getting set
}
data.body = ReactDOM.renderToString(component);
data.css = css.join('');
data.langFile = Translator.resolveLangFile(lang, req);
data.initialState = JSON.stringify(store.getState());
data.langCode = lang;
});
if (redirected) return; // already redirected, don't send html