为什么节点js中的云功能无法正确部署

时间:2019-02-11 14:49:36

标签: node.js firebase google-cloud-functions

在这里,我使用云功能来创建用户,而当我尝试将其部署到云功能时,我正在使用快速模块,并显示错误消息:功能未正确部署

const express = require('express');
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const serviceaccount = require('./ServiceAccountKey.json');

const app = express();

admin.initializeApp({
credential: admin.credential.cert(serviceaccount)
});


app.post('/',(req,res)=>{
if(req.method!== 'POST'){
    res.status(400).send("what are u trying baby");
    return;
}
admin.auth().createUser({
    email:req.body.email,
    password: req.body.pass,
}).then(function(userRecord){
    res.send({'uid': userRecord.uid});
    return;
}).catch(function(error){
    res.send({'error': 'Try COrrect one baby'});
    return;
});
return;
});
exports.register = funtions.Https.onRequest(app);

当我在末尾添加

module.exports = {
app
}

它显示已部署的功能,但未显示在云功能仪表板中

我在这里做什么错

这是我得到我cnat的错误,错误是

⚠  functions[register(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file index.js     can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'express'
at Function.Module._resolveFilename (module.js:476:15)
at Function.Module._load (module.js:424:25)
at Module.require (module.js:504:17)
at require (internal/module.js:20:19)
at Object.<anonymous> (/user_code/index.js:1:79)
at Module._compile (module.js:577:32)
at Object.Module._extensions..js (module.js:586:10)
at Module.load (module.js:494:32)
at tryModuleLoad (module.js:453:12)
at Function.Module._load (module.js:445:3)


Functions deploy had errors with the following functions:
    register

2 个答案:

答案 0 :(得分:0)

您的代码从不使用firebase-functions模块来声明Cloud Function。您的functions变量未使用。您不能只导出期望其运行的任何功能-它必须由SDK构建。

如果要部署Express应用程序,则需要通过HTTP类型函数将其导出:

exports.app = functions.https.onRequest(app);

答案 1 :(得分:0)

首先,我认为您需要更改url的端点。不要只输入'/'。也许像'/ user / create'。

app.post('/user/create',(req,res)=>{
  if(req.method!== 'POST'){
    res.status(400).send("what are u trying baby");
    return;
  }
  admin.auth().createUser({
    email:req.body.email,
    password: req.body.pass,
  }).then(function(userRecord){
     res.send({'uid': userRecord.uid});
     return;
  }).catch(function(error){
    res.send({'error': 'Try COrrect one baby'});
    return;
  });
  return;
});

exports.register = funtions.Https.onRequest(app);

然后在您的firebase.json中,应重写网址:

{
  "functions": {
       ...
   },
  "hosting": {
     "public": "public",
     "rewrites": [
        { 
          "source": "/user/create",
          "function": "register" 
        }
     ]
  }
}

有关更多说明,请遵循此tutorial

相关问题