我想使用 MongoDB 数据库在 firebase 上部署我的 Node.js 应用程序。但我得到的只是 firebase 欢迎页面,而不是我的网站
Posted
技术标签:
【中文标题】我想使用 MongoDB 数据库在 firebase 上部署我的 Node.js 应用程序。但我得到的只是 firebase 欢迎页面,而不是我的网站【英文标题】:I wanna deploy my Node.js app on firebase using MongoDB database. But I'm getting just firebase welcome page, not my website 【发布时间】:2021-12-25 21:11:00 【问题描述】:我的项目显示我在 firebase 上“成功部署”。但是,当我单击该链接时,它只显示了 firebase 欢迎页面,而不是我的项目。图片链接在这里:
这里是 app.js 文件:
//jshint esversion:6
const functions = require("firebase-functions");
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const mongoose = require('mongoose');
const homeStartingContent = "Lacus vel facilisis volutpat est velit egestas dui id ornare. Semper auctor neque vitae tempus quam. Sit amet cursus sit amet dictum sit amet justo. Viverra tellus in hac habitasse. Imperdiet proin fermentum leo vel orci porta. Donec ultrices tincidunt arcu non sodales neque sodales ut. Mattis molestie a iaculis at erat pellentesque adipiscing. Magnis dis parturient montes nascetur ridiculus mus mauris vitae ultricies. Adipiscing elit ut aliquam purus sit amet luctus venenatis lectus. Ultrices vitae auctor eu augue ut lectus arcu bibendum at. Odio euismod lacinia at quis risus sed vulputate odio ut. Cursus mattis molestie a iaculis at erat pellentesque adipiscing.";
const aboutContent = "Hac habitasse platea dictumst vestibulum rhoncus est pellentesque. Dictumst vestibulum rhoncus est pellentesque elit ullamcorper. Non diam phasellus vestibulum lorem sed. Platea dictumst quisque sagittis purus sit. Egestas sed sed risus pretium quam vulputate dignissim suspendisse. Mauris in aliquam sem fringilla. Semper risus in hendrerit gravida rutrum quisque non tellus orci. Amet massa vitae tortor condimentum lacinia quis vel eros. Enim ut tellus elementum sagittis vitae. Mauris ultrices eros in cursus turpis massa tincidunt dui.";
const contactContent = "Scelerisque eleifend donec pretium vulputate sapien. Rhoncus urna neque viverra justo nec ultrices. Arcu dui vivamus arcu felis bibendum. Consectetur adipiscing elit duis tristique. Risus viverra adipiscing at in tellus integer feugiat. Sapien nec sagittis aliquam malesuada bibendum arcu vitae. Consequat interdum varius sit amet mattis. Iaculis nunc sed augue lacus. Interdum posuere lorem ipsum dolor sit amet consectetur adipiscing elit. Pulvinar elementum integer enim neque. Ultrices gravida dictum fusce ut placerat orci nulla. Mauris in aliquam sem fringilla ut morbi tincidunt. Tortor posuere ac ut consequat semper viverra nam libero.";
const profileContent = "Scelerisque eleifend donec pretium vulputate sapien. Rhoncus urna neque viverra justo nec ultrices. Arcu dui vivamus arcu felis bibendum. Consectetur adipiscing elit duis tristique. Risus viverra adipiscing at in tellus integer feugiat. Sapien nec sagittis aliquam malesuada bibendum arcu vitae. Consequat interdum varius sit amet mattis. Iaculis nunc sed augue lacus. Interdum posuere lorem ipsum dolor sit amet consectetur adipiscing elit. Pulvinar elementum integer enim neque. Ultrices gravida dictum fusce ut placerat orci nulla. Mauris in aliquam sem fringilla ut morbi tincidunt. Tortor posuere ac ut consequat semper viverra nam libero.";
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded(extended: true));
app.use(express.static("public"));
mongoose.connect("mongodb+srv://admin-username:<password>@cluster0.quojn.mongodb.net/blogDB", useNewUrlParser: true);
const postSchema =
title: String,
content: String
;
const Post = mongoose.model("Post", postSchema);
app.get("/", function(req, res)
Post.find(, function(err, posts)
res.render("home",
startingContent: homeStartingContent,
posts: posts
);
);
);
app.get("/profile",function(req,res)
Post.find(, function(err, posts)
res.render("profile",
profileContent:profileContent,
posts: posts
);
);
);
app.post("/delete",function(req,res)
const deletedpostID = req.body.delbutton;
Post.findByIdAndRemove(deletedpostID,function(err)
if(!err)
console.log("success");
res.redirect("/");
);
)
app.get("/compose", function(req, res)
res.render("compose");
);
app.post("/compose", function(req, res)
const post = new Post(
title: req.body.postTitle,
content: req.body.postBody
);
post.save(function(err)
if (!err)
res.redirect("/");
);
);
app.get("/posts/:postId", function(req, res)
const requestedPostId = req.params.postId;
Post.findOne(_id: requestedPostId, function(err, post)
res.render("post",
title: post.title,
content: post.content
);
);
);
app.get("/about", function(req, res)
res.render("about", aboutContent: aboutContent);
);
app.get("/contact", function(req, res)
res.render("contact", contactContent: contactContent);
);
app.listen(3000, function()
console.log("Server started on port 3000");
);
firebase.json 文件:
"hosting":
"public": "public",
"rewrites": [
"source": "**",
"function": "app"
],
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
]
,
"functions":
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
],
"source": "functions"
我也尝试过使用 firebase 功能进行部署,但在 cmd 终端中显示了很多错误:
=== Deploying to 'blogsite-project-e6563'...
i deploying functions, hosting
Running command: npm --prefix "$RESOURCE_DIR" run lint
> lint
> eslint .
C:\Users\HP\Documents\Blogsite-project\functions\index.js
6:7 error 'ejs' is assigned a value but never used no-unused-vars
9:1 error This line has a length of 703. Maximum allowed is 80 max-len
10:1 error This line has a length of 564. Maximum allowed is 80 max-len
11:1 error This line has a length of 670. Maximum allowed is 80 max-len
12:1 error This line has a length of 670. Maximum allowed is 80 max-len
25:18 error Missing trailing comma comma-dangle
30:32 error Block must not be padded by blank lines padded-blocks
30:32 error Missing space before opening brace space-before-blocks
32:37 error Missing space before opening brace space-before-blocks
35:19 error Missing trailing comma comma-dangle
36:1 error Expected indentation of 4 spaces but found 6 indent
40:19 error A space is required after ',' comma-spacing
40:32 error A space is required after ',' comma-spacing
40:37 error Block must not be padded by blank lines padded-blocks
40:37 error Missing space before opening brace space-before-blocks
41:1 error Trailing spaces not allowed no-trailing-spaces
42:37 error Missing space before opening brace space-before-blocks
44:1 error Expected indentation of 6 spaces but found 4 indent
44:20 error Missing space before value for key 'profileContent' key-spacing
45:1 error Expected indentation of 6 spaces but found 4 indent
45:17 error Missing trailing comma comma-dangle
50:20 error A space is required after ',' comma-spacing
50:33 error A space is required after ',' comma-spacing
50:38 error Missing space before opening brace space-before-blocks
51:43 error Missing semicolon semi
53:40 error There should be no space before ',' comma-spacing
53:55 error Missing space before opening brace space-before-blocks
54:5 error Expected space(s) after "if" keyword-spacing
54:13 error Missing space before opening brace space-before-blocks
59:3 error Missing semicolon semi
61:39 error Missing space before opening brace space-before-blocks
65:40 error Missing space before opening brace space-before-blocks
68:31 error Missing trailing comma comma-dangle
72:26 error Missing space before opening brace space-before-blocks
73:14 error Missing space before opening brace space-before-blocks
74:1 error Expected indentation of 6 spaces but found 8 indent
79:45 error Block must not be padded by blank lines padded-blocks
79:45 error Missing space before opening brace space-before-blocks
81:1 error Expected indentation of 2 spaces but found 0 indent
83:59 error Missing space before opening brace space-before-blocks
86:28 error Missing trailing comma comma-dangle
91:37 error Missing space before opening brace space-before-blocks
95:39 error Missing space before opening brace space-before-blocks
✖ 43 problems (43 errors, 0 warnings)
38 errors and 0 warnings potentially fixable with the `--fix` option.
events.js:377
throw er; // Unhandled 'error' event
^
Error: spawn npm --prefix "%RESOURCE_DIR%" run lint ENOENT
at notFoundError (C:\Users\HP\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:6:26)
at verifyENOENT (C:\Users\HP\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:40:16)
at ChildProcess.cp.emit (C:\Users\HP\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:27:25)
at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
Emitted 'error' event on ChildProcess instance at:
at ChildProcess.cp.emit (C:\Users\HP\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\cross-env\node_modules\cross-spawn\lib\enoent.js:30:37)
at Process.ChildProcess._handle.onexit (internal/child_process.js:277:12)
code: 'ENOENT',
errno: 'ENOENT',
syscall: 'spawn npm --prefix "%RESOURCE_DIR%" run lint',
path: 'npm --prefix "%RESOURCE_DIR%" run lint',
spawnargs: []
Error: functions predeploy error: Command terminated with non-zero exit code1
我在互联网上搜索了为什么会发生这种情况。请帮助解决此错误。将不胜感激。
【问题讨论】:
【参考方案1】:查看日志的结尾。
错误:函数预部署错误:命令以非零退出代码 1 终止
由于 eslint 错误,代码未部署。解决这些问题并再次尝试部署。
【讨论】:
以上是关于我想使用 MongoDB 数据库在 firebase 上部署我的 Node.js 应用程序。但我得到的只是 firebase 欢迎页面,而不是我的网站的主要内容,如果未能解决你的问题,请参考以下文章
在使用 node.js 完成所有承诺后,我想在 mongodb 中插入数据
如何在 Node 应用程序中使用 `mongoDB` 数据进行分析
我想使用日期范围“utc_timestamp”字段从 mongodb 集合中获取结果?
尝试使用 R 和 mongodb 在 ggplot 中制作条形图
我想使用 MongoDB 数据库在 firebase 上部署我的 Node.js 应用程序。但我得到的只是 firebase 欢迎页面,而不是我的网站