Netlify lambda函数返回“在完成请求之前退出进程”错误

时间:2019-01-27 17:50:31

标签: node.js express serverless netlify

我有一个lambda函数,该函数在本地运行良好,但是当我将代码推送到Netlify并访问/.netlify/functions/download端点时,出现以下错误:

{
    "errorMessage": "RequestId: 8a8f48f8-4bc3-4435-8b74-e6bd3df4e682 Process exited before completing request"
}

我想念什么?

这是我的lambda函数:

const express = require('express');
const serverless = require('serverless-http');
const functionName = 'download';
const app = expressApp(functionName);

export default function expressApp(functionName) {

    const app = express();
    const router = express.Router();

    // Local path
    // const routerBasePath = `/${functionName}`;

    // Netlify path
    const routerBasePath = `/.netlify/functions/${functionName}/`;

    const fs = require('fs');
    const mime = require('mime');

    router.get('/', (req, res) => {

        // Local file
        // var filename = '../../Desktop/mypdf.pdf';

        // Staging file
        let filename = '../build_staging/pdf-newName.pdf';

        let mimetype = mime.lookup(filename);
        let filestream = fs.createReadStream(filename);

        res.setHeader('Content-disposition', 'attachment; filename=mypdf.pdf');
        res.setHeader('Content-type', mimetype);

        filestream.pipe(res);
    });

    app.use(routerBasePath, router);
    return app;
}

exports.handler = serverless(app);

谢谢!

0 个答案:

没有答案