Express Web App在第二次提交时丢失500错误

时间:2018-05-11 22:33:10

标签: node.js express asynchronous

一般来说,Express和Nodejs都是新手,我刚刚构建了一个非常简单的应用程序,它在第一次提交时完全按预期工作,但是当重新加载根页面并重新提交时,它会抛出500错误,如图所示在调试日志中:

GET / 304 2230.094 ms - -
GET /stylesheets/main.css 304 10.464 ms - -
GET /success? 200 4309.006 ms - 303
GET /stylesheets/main.css 304 2.027 ms - -
Error: Can't set headers after they are sent.
    at validateHeader (_http_outgoing.js:491:11)
    at ServerResponse.setHeader (_http_outgoing.js:498:3)
    at ServerResponse.header (C:\Users\Hayden\source\repos\fieldops\FieldOps\node_modules\express\lib\response.js:767:10)
    at ServerResponse.send (C:\Users\Hayden\source\repos\fieldops\FieldOps\node_modules\express\lib\response.js:170:12)
    at done (C:\Users\Hayden\source\repos\fieldops\FieldOps\node_modules\express\lib\response.js:1004:10)
    at Object.exports.renderFile (C:\Users\Hayden\source\repos\fieldops\FieldOps\node_modules\pug\lib\index.js:421:12)
    at View.exports.__express [as engine] (C:\Users\Hayden\source\repos\fieldops\FieldOps\node_modules\pug\lib\index.js:464:11)
    at View.render (C:\Users\Hayden\source\repos\fieldops\FieldOps\node_modules\express\lib\view.js:135:8)
    at tryRender (C:\Users\Hayden\source\repos\fieldops\FieldOps\node_modules\express\lib\application.js:640:10)
    at Function.render (C:\Users\Hayden\source\repos\fieldops\FieldOps\node_modules\express\lib\application.js:592:3)
**GET /success? 500 128.822 ms - 150**
GET /stylesheets/main.css 304 0.843 ms - -

此行为在我的开发环境和应用程序的容器化版本中都会持续存在。在两种情况下重新启动服务器都会解决它,这让我相信我没有正确完成线程。这主要是在一条路线和一块中间件中处理的:

router.get('/', asyncHandler(async (req, res, next) => {
    const newEnv = await skytap.environments.create({ template_id: 1334141 });
    var environments = await skytapV2.environments.list(newEnv);
    skytap.environments.update({ configuration_id: newEnv.id, name: 'New Test Space' }, next);
    skytap.environments.start({ configuration_id: newEnv.id }, next);
    envID = newEnv.id;
    res.render('success', { title: 'Success!', env: newEnv.id, erl: newEnv.url });

    next();
}));

/* POST Schedule */
router.use(asyncHandler(async (req, res, next) => {
    var isRunning = await skytap.environments.waitForState({ configuration_id: envID, runstate: 'running' }, next);
    var dt = datetime.create();
    var formatTime = dt.format('Y/m/d H:M');
    dt.offsetInHours(1);
    var formatOffsetTime = dt.format('Y/m/d H:M');       
    var postData = {
        title: "Killer Schedule",
        configuration_id: envID,
        actions: [
            {
                type: "shut down",
                offset: "3600"
            }
        ],
        start_at: formatTime,
        time_zone: "UTC",
        end_at: formatOffsetTime,
        time_zone: "UTC",
        delete_at_end: true
    };
    var options = {
        url: 'https://cloud.skytap.com/schedules.json',
        method: 'POST',
        headers: {
            'content-type': 'application/json',
            'Authorization': 'Basic *auth key*'
        },
        body: postData,
        json:true
    };
    request(options, function (err, res, body) {
        if (err) {
            console.error('error posting json: ', err)
            throw err
        }
        var headers = res.headers
        var statusCode = res.statusCode
        console.log('headers: ', headers)
        console.log('statusCode: ', statusCode)
        console.log('body: ', body)
    });
    next();
}));

最初我认为我的数据库调用没有被设置为非阻塞,但即使删除了该行为仍然存在。我在这里缺少什么?

0 个答案:

没有答案