[英]How to make use of file hash or its use it downloading a file to a server Using Node JS
[英]How to make server connection in separate file and use other files as a function in main server file using node Js?
我正在使用 mongo db 為數據庫制作 node.js 應用程序。 我正在所有文件中創建服務器並且它運行良好。 但是當我運行整個項目時,每次調用函數時它都會創建服務器。 誰能指導我如何在一個文件中創建服務器和數據庫連接並將其他 Api 作為函數調用? 我試過這個,但這是行不通的。 當我從 follower api 文件調用函數 follower 時,它不會進入函數內部,也不會打印控制台。 請幫助我
服務器.js
var mongo = require('mongodb');
var resultant;
var http = require('http');
var bodyParser = require('body-parser');
var qs = require('querystring');
var common = require('./followersapi');
var db = require('./dbconnection');
http.createServer(function (req, res)
{
res.writeHead(200, {"Content-Type": "application/json"});
console.log('Server running at http://127.0.0.1:9001/');
common.follower();
db.connectToServer();
}).listen(9080);
數據庫連接.js
var MongoClient = require( 'mongodb' ).MongoClient;
var _db;
module.exports = {
connectToServer: function( callback ) {
MongoClient.connect( "mongodb://localhost:27017/background_wallpapers", function( err, db ) {
if (!err) {
console.log("We are connected");
}
else
console.log("We are not connected");
_db = db;
return callback( err );
} );
},
getDb: function() {
return _db;
}
};
追隨者api.js
module.exports = {
follower: function () {
console.log("hello");
}
};
請在 createServer 之前測試這個使用 db.connection
var db = require('./dbconnection');
db.connectToServer();
http.createServer(function (req, res)
{
res.writeHead(200, {"Content-Type": "application/json"});
console.log('Server running at http://127.0.0.1:9001/');
common.follower();
}).listen(9080);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.