0
我已經使用了下面的代碼。我已經運行該文件,並沒有錯誤。但服務器192.123.1.134:5001顯示'沒有收到數據'。 請幫助我找出代碼中的錯誤。節點服務器未運行
express = require('express');
app = express();
config = require('./config/database.js');
var path = require('path');
//app.set('views', __dirname + '/views')
app.set('views', path.join(__dirname, '/views'));
app.set('view engine', 'jade');
router = express.Router();
var routes = require('./user/index');
var admin = require('./user/user');
app.use('/', routes);
app.use('/user', admin);
http = require('http');
server = http.createServer(app);
server.listen('5001','192.123.1.134', function(){
console.log('%s: Node server started on %s:%d ...',Date(Date.now()));
});
index.js
var User = require('../model/user.js');
router.get('/', function(req, res, next) {
User.getuserdetails(function(data) {
res.render('user', {
title: 'Welcome',
result : data
});
});
//res.render('index', { title: 'welcome' });
});
module.exports = router;
user.js的(模型)
var connection =require("../config/database.js");;
router.getuserdetails = function (req,res){
var result = connection.query('SELECT * FROM table1','', function (err, rows) {
if (err) throw err
});
}
module.exports = router;
database.js
var mysql = require('mysql');
var connection = mysql.createConnection({
host : 'localhost',
user : 'root',
password : 'aaa',
database : 'my_db'
});
connection.connect(function(err) {
if (err) throw err;
});
module.exports = connection;
你在哪裏運行這個程序?它是localhost還是一些外部服務器? – abdulbarik
*「沒有錯誤」* - 您不是在尋找錯誤。使用Node,您需要偵聽[錯誤事件](https://nodejs.org/api/net.html#net_event_error)才能獲得任何反饋。 – poke
@abdulbarik我在虛擬主機服務器上運行192.123.1.134 –