我在NodeJS中用Serverless创建了一个lambda函数处理程序。当我使用命令:serverless offline start时,我没有收到任何错误。但是,当我想使用无服务器部署命令部署应用程序时,它可以正常部署。当我想要去端点时,我收到内部服务器错误,只有在我的应用程序中需要camaro时才会发生这种情况。我需要camaro库来从XML创建模板。
我尝试使用节点6.10并删除camaro,并使用节点6.10安装它。这没有什么区别。
这是我可以在Cloud watch中查看的错误:
模块初始化错误:错误(本机)时出错 Module.load中的Object.Module._extensions..node(module.js:597:18) (module.js:487:32)在tryModuleLoad(module.js:446:12)at at Module.require中的Function.Module._load(module.js:438:3) (module.js:497:17)at require(internal / module.js:20:19)at 宾语。 (/var/task/node_modules/camaro/index.js:4:16)at Object.Module._extensions..js中的Module._compile(module.js:570:32) (module.js:579:10)
这是我的index.js
const serverless = require('serverless-http');
///
var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var index = require('./routes/index');
var users = require('./routes/users');
var app = express();
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');
// uncomment after placing your favicon in /public
//app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use('/', index);
app.use('/users', users);
// catch 404 and forward to error handler
app.use(function(req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
});
// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;
//serverless
module.exports.handler = serverless(app);
这是我的serverless.yml
# serverless.yml
service: lambda-dashboardcb
provider:
name: aws
runtime: nodejs6.10
stage: dev
region: eu-west-1
functions:
app:
handler: index.handler
events:
- http: ANY /
- http: 'ANY {proxy+}'
plugins:
- serverless-offline
这是我的package.json
package.json
{
"name": "lambda-dashboardcb",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon ./bin/www"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^0.17.1",
"body-parser": "^1.18.2",
"camaro": "^2.2.2",
"cookie-parser": "^1.4.3",
"debug": "~2.6.9",
"express": "^4.15.5",
"google-oauth-jwt": "^0.2.0",
"googleapis": "^23.0.0",
"morgan": "^1.9.0",
"pug": "^2.0.0-beta11",
"serve-favicon": "^2.4.5",
"serverless-http": "^1.5.2"
},
"devDependencies": {
"nodemon": "^1.13.3",
"serverless-offline": "^3.16.0"
}
}
路由器
var express = require('express');
var router = express.Router();
var metadata = require('../public/javascripts/metadata-onix.js');
var path = require("path");
var app = express();
/* GET home page. */
router.get('/', function(req, res, next) {
res.sendFile(path.join(__dirname, '../public', 'index.html'));
});
router.param('isbn', function (req,res,next){
next();
});
router.get('/metadata:isbn', function(req,res,next){
/**
- Get ISBN From URL
- Get Metadata from ISBN, return as JSON
**/
var isbn = req.params.isbn;
var info = metadata.getMetadataOnix(isbn).then(function(info) {
res.json({ cover : info });
});
});
module.exports = router;
使用Camaro的模块
const axios = require('axios');
const transform = require('camaro');
exports.getMetadataOnix = function (id) {
/**
- Create template
- Get request to content CB API.
- Transform data with template.
- Return data.
**/
const template = {
template stuff...
}
return axios({
method: 'get',
url: 'APIURL'+id,
transformResponse: [function (data) {
"use strict";
data = transform(data, template);
return data;
}],
// No .catch here
}).then(function(resp){
console.log('Cover from book:'+JSON.stringify(resp.data));
return resp.data;
});
}
答案 0 :(得分:2)
camaro是一个原生模块。您需要安装正确的prebuilt binary for AWS Lambda。
即使您在本地切换到节点6.10,但机器上安装的二进制文件仅为您的平台构建,这可能与AWS Lambda上的平台不同。
为了在AWS Lambda上使用camaro,您应该从Releases下载预构建的camaro副本,并将其放到此文件夹路径node_modules / camaro / lib / binding / camaro.node。
截至目前,AWS Lambda仅支持Linux上的节点6,因此您正在寻找camaro-v2.1.0-node-v48-linux-x64.tar.gz。
Lambda现在已经支持Node 8,因此请相应地选择预先构建的二进制文件。