Javascript 如何使用参数向静态资源发出请求?
我有我的node.js restify服务器和带有静态资源的文件夹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
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'
}));
可能重复