我正在使用教程,因此添加了以下代码
var express = require('express'); // Web Framework
var app = express();
var sql = require('mssql'); // MS Sql Server client
// Connection string parameters.
var sqlConfig = {
user: 'student',
password: 'something',
server: 'localhost',
database: 'Raw_Data'
}
// Start server and listen on http://localhost:8081/
var server = app.listen(8081, function () {
var host = server.address().address
var port = server.address().port
console.log("app listening at http://%s:%s", host, port)
})
app.get('/', function (req, res) {
sql.connect(sqlConfig, function() {
var request = new sql.Request();
request.query('select * from testtable', function(err, recordset) {
if(err) console.log(err);
res.end(JSON.stringify(recordset)); // Result in JSON format
});
});
})
使用节点server.js启动服务器后,当我访问localhost:8081时收到一条错误语句Cannot GET /。你有什么主意吗?
答案 0 :(得分:0)
您需要创建一个节点路由器来重定向/
中的请求,该请求在您的代码中丢失
var express = require('express');
var app = express();
var router = express.Router();
/*other code*/
//create a router for /
router.get('/', function (req, res) {
sql.connect(sqlConfig, function() {
var request = new sql.Request();
request.query('select * from testtable', function(err, recordset) {
if(err) console.log(err);
res.end(JSON.stringify(recordset)); // Result in JSON format
});
});
});