gpt4 book ai didi

node.js - MEAN 堆栈应用程序中无法获取/错误

转载 作者:太空宇宙 更新时间:2023-11-04 01:34:20 25 4
gpt4 key购买 nike

我正在关注Heroku的教程:https://devcenter.heroku.com/articles/mean-apps-restful-api我遵循了每一步,没有部署到 Heroku 服务器并在我的本地主机中工作。因此,当我使用端口 localhost/8080 打开应用程序时,我发现错误 (Cannot Get/)。这是我的服务器代码:

var express = require("express");
var bodyParser = require("body-parser");
var mongodb = require("mongodb");
var ObjectID = mongodb.ObjectID;

var CONTACTS_COLLECTION = "contacts";

var app = express();
app.use(bodyParser.json());

// Create link to Angular build directory
var distDir = __dirname + "/dist/";
app.use(express.static(distDir));

// Create a database variable outside of the database connection callback to reuse the connection pool in your app.
var db;

// Connect to the database before starting the application server.
mongodb.MongoClient.connect(process.env.MONGODB_URI || "mongodb://localhost:27017/test", { useNewUrlParser: true }, function (err, client) {
if (err) {
console.log(err);
process.exit(1);
}

// Save database object from the callback for reuse.
db = client.db();
console.log("Database connection ready");

// Initialize the app.
var server = app.listen(process.env.PORT || 8080, function () {
var port = server.address().port;
console.log("App now running on port", port);
});
});

// CONTACTS API ROUTES BELOW

// Generic error handler used by all endpoints.
function handleError(res, reason, message, code) {
console.log("ERROR: " + reason);
res.status(code || 500).json({"error": message});
}

/* "/api/contacts"
* GET: finds all contacts
* POST: creates a new contact
*/

app.get("/api/contacts", function(req, res) {
db.collection(CONTACTS_COLLECTION).find({}).toArray(function(err, docs) {
if (err) {
handleError(res, err.message, "Failed to get contacts.");
} else {
res.status(200).json(docs);
}
});
});



app.post("/api/contacts", function(req, res) {
var newContact = req.body;
newContact.createDate = new Date();

if (!req.body.name) {
handleError(res, "Invalid user input", "Must provide a name.", 400);
} else {
db.collection(CONTACTS_COLLECTION).insertOne(newContact, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to create new contact.");
} else {
res.status(201).json(doc.ops[0]);
}
});
}
});

/* "/api/contacts/:id"
* GET: find contact by id
* PUT: update contact by id
* DELETE: deletes contact by id
*/

app.get("/api/contacts/:id", function(req, res) {
db.collection(CONTACTS_COLLECTION).findOne({ _id: new ObjectID(req.params.id) }, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to get contact");
} else {
res.status(200).json(doc);
}
});
});

app.put("/api/contacts/:id", function(req, res) {
var updateDoc = req.body;
delete updateDoc._id;

db.collection(CONTACTS_COLLECTION).updateOne({_id: new ObjectID(req.params.id)}, updateDoc, function(err, doc) {
if (err) {
handleError(res, err.message, "Failed to update contact");
} else {
updateDoc._id = req.params.id;
res.status(200).json(updateDoc);
}
});
});

app.delete("/api/contacts/:id", function(req, res) {
db.collection(CONTACTS_COLLECTION).deleteOne({_id: new ObjectID(req.params.id)}, function(err, result) {
if (err) {
handleError(res, err.message, "Failed to delete contact");
} else {
res.status(200).json(req.params.id);
}
});
});

PS:当我运行命令 ngserve 时:主页显示但与数据库没有连接,它只是一个静态页面..请帮助我是这种卑鄙语言的初学者。

最佳答案

它似乎遗漏了您的服务器代码中的一部分。您已经配置了 API 部分,该部分可以由您的 Angular 应用程序调用,但您并未为您的 Angular 应用程序提供服务。

您应该监听所有 GET 请求并仅将 index.html 作为文件发送,该文件实际上应该包含您的 Angular SPA。像这样的事情:

app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'index.html'));
});

您可以查看 Express 文档:http://expressjs.com/fr/api.html#res.sendFile

关于node.js - MEAN 堆栈应用程序中无法获取/错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55046730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com