如果您使用firebase serve在本地主机上进行测试,则我的Node应用程序将完美运行。如果我运行firebase serve,则localhost:5000 / blogs将呈现我的网站。
但是,在Firebase部署之后,如果我转到https://garylai-b77af.firebaseapp.com/blogs,它会显示“错误:无法处理该请求”。
预先感谢您的帮助!
这是我的index.js
const functions = require('firebase-functions');
var bodyParser = require("body-parser"),
mongoose = require("mongoose"),
express = require("express"),
app = express();
//App Config
mongoose.connect('//the correct url, username and password to mlab omitted here');
app.set("view engine", "ejs");
app.use(express.static("public"));//for serving custom stylesheet
app.use(bodyParser.urlencoded({extended: true}));
var blogSchema = new mongoose.Schema({
title: String,
image: String,
body: String,
snippet: String,
created: {type: Date, default: Date.now}
});
var Blog = mongoose.model("Blog", blogSchema);
//INDEX ROUTE
app.get("/blogs", function(req, res){
Blog.find({}, function(err, blogs){
if(err){console.log("Error!");}
else {
res.render("index", {blogs: blogs});//pass blogs the data under the name blogs to index
}
})});
//Restful Routes
app.get("/", function(req, res){
res.redirect("/blogs");
});
//SHOW ROUTE
app.get("/blogs/:id", function(req, res){
Blog.findById(req.params.id, function(err, foundBlog){
if(err){
res.redirect("/blogs");
} else{
res.render("show", {blog: foundBlog});
}
})
})
exports.app = functions.https.onRequest(app);