Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用参数向静态资源发出请求?_Javascript_Node.js_Restify_Staticresource - Fatal编程技术网

Javascript 如何使用参数向静态资源发出请求?

Javascript 如何使用参数向静态资源发出请求?,javascript,node.js,restify,staticresource,Javascript,Node.js,Restify,Staticresource,我有我的node.js restify服务器和带有静态资源的文件夹 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.serveSt

我有我的node.js restify服务器和带有静态资源的文件夹

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'
    }));
我试图理解如何使用localhost:8080/index.html?token=123等参数向index.html发出get请求


如果令牌有效,则将index.html返回给客户端,否则返回error

您可以链接多个请求处理程序和
next()
方法-首先执行一些参数的验证,然后作为第二个处理程序,使用
serveStatic
方法。下面是一个例子:

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'
}));
可能重复