Node表示问题,在浏览器中获取以下“无法GET / test”

时间:2017-04-28 17:04:42

标签: node.js

我有以下节点表达功能:

var express = require('express');
var app = express();

// Listen on port 8000, IP defaults to 127.0.0.1
var server = app.listen(8000, function() {
    console.log("node express app started at http://localhost:8000");
});

app.get("/test", 
  function(error, request, response, next) {

    request.set("Content-Type", "text/html; charset=UTF-8");

    if (error) {
        request.status(403);
        return next();
    } 
        var id = request.query.snuid;
        var currency = request.query.currency;
        var mac_address = request.query.mac_address;
        var display_multiplier = request.query.display_multiplier;
        //
        request.status(200);
});

当我加载浏览器并输入url:

http://localhost:8000/test?snuid=1234&currency=1000&mac_address=00-16-41-34-2C-A6&display_multiplier=1.0

我不知道为什么我会收到此错误!?不确定,它应该基于我看到的文档和示例。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:1)

我认为是因为你想使用中间件而不是路由,所以也许你的代码可以这样写:

var express = require('express');
var app = express();


app.use(function(err, request, response, next) {
    if (error) {
        request.status(403);
        return next();
    } 
});

app.get("/test", function(request, response, next) {

response.set("Content-Type", "text/html; charset=UTF-8");

var id = request.query.snuid;
var currency = request.query.currency;
var mac_address = request.query.mac_address;
var display_multiplier = request.query.display_multiplier;
    //
    response.status(200).end();
});

// Listen on port 8000, IP defaults to 127.0.0.1
var server = app.listen(8000, function() {
  console.log("node express app started at http://localhost:8000");
});`