我正在关注Heroku的教程:https://devcenter.heroku.com/articles/mean-apps-restful-api 我遵循了没有部署到Heroku服务器的所有步骤,并在本地主机上工作。 因此,当我使用端口localhost / 8080打开应用程序时,我发现了(无法获取/)错误。 这是我的服务器代码:
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:当我运行命令ng serve时:主页显示,但与数据库无连接,它只是一个静态页面..请帮助 我是这种卑鄙语言的初学者。
答案 0 :(得分:0)
似乎它丢失了服务器代码中的一部分。 您已经配置了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