0
我開發了一個簡單的MEAN stack CRUD應用程序。我將我的網站部署到了天藍色的網站。在我的本地主機應用程序工作正常。但是我想連接到mongoLab數據庫。我如何連接它?如何連接到mongoLab?
這是server.js
var express = require('express');
var app = express();
var mongojs = require('mongojs');
var db = mongojs('posts', ['posts']);
var bobyParser = require('body-parser');
app.use(express.static(__dirname + "/public"));
app.use(bobyParser.json());//
app.get('/posts', function (req, res) {
console.log("I got the server request!")
db.posts.find(function (err, docs) {
console.log(docs);
res.json(docs);
});
});
app.post('/posts', function (req, res) {
console.log(req.body);
//Insert - This is the first part of the CRUD
db.posts.insert(req.body, function (err, doc) {
res.json(doc);
});
});
app.delete('/posts/:id', function (req, res) {
var id = req.params.id;
console.log(id);
db.posts.remove({ _id: mongojs.ObjectId(id) }, function (err, doc) {
res.json(doc);
});
});
app.get('/posts/:id', function (req, res) {
var id = req.params.id;
console.log(id);
db.posts.findOne({ _id: mongojs.ObjectId(id) }, function (err, doc) {
res.json(doc);
});
});
app.put('/posts/:id', function (req, res) {
var id = req.params.id;
console.log(req.body.name);
db.posts.findAndModify({
query: { _id: mongojs.ObjectId(id) },
update: { $set: { name: req.body.name, info: req.body.info, twitter: req.body.twitter }},
new: true
}, function (err, doc) {
res.json(doc);
});
});
app.listen(3000);
console.log("Server running from port 3000");
代碼我怎樣才能進行連接?我有天青提供的連接信息。 非常感謝。