错误信息:使用serverless将nodejs API部署到awsapigateway后出现内部服务器错误

n3schb8v  于 2022-12-18  发布在  Node.js
关注(0)|答案(1)|浏览(163)

我的nodejs应用程序在我的本地没有serverless的情况下工作得很好。但是当我使用serverless.yml配置部署相同的应用程序时,我得到了这个错误“内部服务器错误”。我试图在我的s3bucket中上传文件。我不确定我在我的serverless中是否缺少任何权限相关的代码。请帮助。
我的serverless.yml

service: nodejss3uploader

# Check out our docs for more details
frameworkVersion: '3'

provider:
  name: aws
  runtime: nodejs16.x
  memorySize: 2048
  stage: prod
  timeout: 15
  region: us-east-1
  iam:
    role:
      statements:
        # Allow functions to list all buckets
        - Effect: Allow
          Action: 's3:ListBucket'
          Resource: '*'
        # Allow functions to read/write objects in a bucket
        - Effect: Allow
          Action:
            - 's3:GetObject'
            - 's3:PutObject'
          Resource:
            - 'arn:aws:s3:::bucket-assets/*'

functions:
  uploadtoS3:
    handler: handler.uploadtoS3
    events:
      - http: 
          path: upload/aws
          method: POST
    environment:
      AWS_ACCESS_ID: 123654123654123
      AWS_SKEY: awesomeawskeypackage
      AWS_S3_BUCKET: bucket-assets

处理程序. js代码

'use strict';
const app = require('./bin/www');
const serverless = require('serverless-http');
module.exports.handler = serverless(app);

www.js

var app = require('../app');
var debug = require('debug')('fileuploader:server');
var http = require('http');

var port = normalizePort(process.env.PORT || '80');
app.set('port', port);

var server = http.createServer(app);

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

dldeef67

dldeef671#

我已经设法解决了这个问题。
我不得不将我的hander.js代码更改为。我导入了app.js文件,而不是www.js。app.js代码具有需要执行的函数。

'use strict';
const app = require('./app');
const serverless = require('serverless-http');
module.exports.uploadtoS3 = serverless(app)

app.js

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
let bodyParser = require('body-parser');

var indexRouter = require('./routes/index');
var uploaderRoute = require('./routes/upload.route');
var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
/* app.use(express.json());
app.use(express.urlencoded({ extended: false })); */
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', indexRouter);
app.use('/upload', uploaderRoute);

// catch 404 and forward to error handler
app.use(function(req, res, next) {
  next(createError(404));
});

// 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.yml没有任何变化。这样我就能够执行。

相关问题