Learn2Code
Learn2Code

Reputation: 2280

Node express issue, get the following "Cannot GET /test" in browser

I have the following node express 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 up the browser and enter in the url:

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

Im not sure why I am getting this error!? Not sure, it should work based on the documentation and examples I have seen. Any help would be appreciated.

Upvotes: 1

Views: 733

Answers (1)

FaztJs
FaztJs

Reputation: 71

I think is because you want to use a middleware and not a route, so maybe your code can be write in this way:

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

Upvotes: 1

Related Questions