Node express problem, get the following "Unable to GET / test" in browser

I have the following express node function:

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);
});

      

When I load my browser and enter the url:

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

      

I'm not sure why I am getting this error !? Not sure if it should work based on the documentation and examples I've seen. Any help would be greatly appreciated.

+3


source to share


1 answer


I think this is because you want to use middleware, not a route, so perhaps your code could write like this:



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");
});`

      

+1


source







All Articles