在 MongoDb 数据库上执行操作时页面挂起
Page hangs while doing operation on MongoDb database
我正在使用 Cloude 9 环境来开发我的 nodejs 应用程序。因为我已经编写了连接到 mongodb 数据库的代码。我正在成功连接到数据库。但是当我尝试对数据库进行操作时,页面变得没有响应并挂起。
下面是我的server.js文件的代码
var Db = require('mongodb').Db;
var http = require('http');
var path = require('path');
var async = require('async');
var socketio = require('socket.io');
var express = require('express');
var ejs = require('ejs');
var app = express();
var helpers = require('express-helpers')
var MongoClient = require('mongodb').MongoClient;
var Server = require('mongodb').Server;
var db;
helpers(app);
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: true})); // for parsing application/x-www-form-urlencoded
var server = http.Server(app);
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function () {
var addr = server.address();
console.log("Chat server listening at", addr.address + ":" + addr.port);
});
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//app.use(express.static(__dirname + '/client'));
app.use(express.static(path.join(__dirname, '/client')));
// MongoDB Connection
app.use(function(req, res, next) {
next();
})
app.post('/ajax-mongo-connect', function (req, res) {
var mongoClient = new MongoClient(new Server('localhost', 27017));
mongoClient.open(function(err, mongoClient) {
if(err){
console.log(err);
}else{
var db = mongoClient.db("mydb");
console.log('database connected',db);
mongoClient.close();
}
})
})
请注意,在我的视图页面中,我正在使用 POST 方法调用操作 ajax-mongo-connect
。调用转到 app.post('/ajax-mongo-connect'...
但页面变得不负责任并挂起。
让我知道我在做什么。
您需要在 /ajax-mongo-connect
路线中 return 某些内容,例如 res.send('its working dude!');
或调用 next()
函数。
干杯!
我正在使用 Cloude 9 环境来开发我的 nodejs 应用程序。因为我已经编写了连接到 mongodb 数据库的代码。我正在成功连接到数据库。但是当我尝试对数据库进行操作时,页面变得没有响应并挂起。
下面是我的server.js文件的代码
var Db = require('mongodb').Db;
var http = require('http');
var path = require('path');
var async = require('async');
var socketio = require('socket.io');
var express = require('express');
var ejs = require('ejs');
var app = express();
var helpers = require('express-helpers')
var MongoClient = require('mongodb').MongoClient;
var Server = require('mongodb').Server;
var db;
helpers(app);
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({extended: true})); // for parsing application/x-www-form-urlencoded
var server = http.Server(app);
server.listen(process.env.PORT || 3000, process.env.IP || "0.0.0.0", function () {
var addr = server.address();
console.log("Chat server listening at", addr.address + ":" + addr.port);
});
app.use(express.static(__dirname + '/public'));
app.set('views', __dirname + '/public/views');
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'html');
//app.use(express.static(__dirname + '/client'));
app.use(express.static(path.join(__dirname, '/client')));
// MongoDB Connection
app.use(function(req, res, next) {
next();
})
app.post('/ajax-mongo-connect', function (req, res) {
var mongoClient = new MongoClient(new Server('localhost', 27017));
mongoClient.open(function(err, mongoClient) {
if(err){
console.log(err);
}else{
var db = mongoClient.db("mydb");
console.log('database connected',db);
mongoClient.close();
}
})
})
请注意,在我的视图页面中,我正在使用 POST 方法调用操作 ajax-mongo-connect
。调用转到 app.post('/ajax-mongo-connect'...
但页面变得不负责任并挂起。
让我知道我在做什么。
您需要在 /ajax-mongo-connect
路线中 return 某些内容,例如 res.send('its working dude!');
或调用 next()
函数。
干杯!