0

I have my node.js restify server, and folder with static resource

const restify = require('restify')

let server = restify.createServer()

server.listen(8080, function () {
    console.log('%s listening at %s', server.name, server.url)
});


server.get('/*', restify.plugins.serveStatic({
        directory: __dirname + '/static',
        default: 'index.html'
    }));

i'm trying to understand how to make get request to index.html with parameters like localhost:8080/index.html?token=123

and if token is valid, return index.html to client, else return error

  • possible duplicate https://stackoverflow.com/questions/15830448/how-to-parse-read-multiple-parameters-with-restify-framework-for-node-js – Nikos M. Dec 21 '18 at 10:39

1 Answers1

1

You can chain multiple request handlers and the next() method - first do some parameters' validation and then, as a second handler, use the serveStatic method. Here's an example:

const restify = require('restify')

let server = restify.createServer()

server.listen(8080, function () {
    console.log('%s listening at %s', server.name, server.url)
});


server.get('/*', (request, response, next) => {
    const token = request.query.token;
    if(token !== '123') {
        //those two lines below will stop your chain and just return 400 HTTP code with some message in JSON
        response.send(400, {message: "Wrong token"});
        next(false); 
        return;
    }
    next(); //this will jump to the second handler and serve your static file
    return;
},
restify.plugins.serveStatic({
    directory: __dirname + '/static',
    default: 'index.html'
}));
Nisar Saiyed
  • 728
  • 1
  • 8
  • 17
Piotr Mitkowski
  • 282
  • 3
  • 13