我正在尝试从节点/express中的URL (localhost:8080/? data =test)获取数据,但无法在server.js文件中捕获。但是不确定为什么在get('/')或get('*')方法中不能捕获。这是我目前使用的代码。
//Importing dependencies
const express = require('express');
var path = require('path');
var fs = require('fs');
var url = require('url');
const querystring = require('querystring');
const port = process.env.PORT ||8081;
//Starting Express app
const app = express();
//Set the base path to the angular-test dist folder
app.use(express.static(path.join(__dirname, 'dist/bybuy')));
//Any routes will be redirected to the angular app
app.get('/', function(req, res) {
console.log("YESSSSSSSSSSSSSSSSSSSSSS",req.url);
var str1="";
if(req.query.data.indexOf(' ')>=0){
var str1=replaceAll(req.query.data,' ','PLUS');
}
res.cookie('accessToken', str1,
{
overwrite: true,
encode: v => v
})
res.sendFile(path.join(__dirname, 'dist/bybuy/index.html'));
});
app.post('/home', function(req, res) {
console.load("LKJHFDLKJDLKJDLKJD");
res.sendFile(path.join(__dirname, 'dist/bybuy/index.html'));
});
app.post('/cashfreeResult',(req, res, next)=>{
console.log("merchantHosted result hit");
res.sendFile(path.join(__dirname, 'dist/bybuy/index.html'));
});
//Starting server on port 8081
;
app.get('*', function(req, res) {
var url_parts = url.parse(req.url);
let parsedQs = querystring.parse(url_parts.query);
console.log("NOOOOOOOOOOOOOOOOOOOO",parsedQs);
res.sendFile(path.join(__dirname, 'dist/bybuy/index.html'));
});
app.listen(port, () => {
console.log('Server started!');
console.log('on port ',port);
})
function replaceAll(string, search, replace) {
return string.split(search).join(replace);
}
发布于 2020-11-06 00:13:43
req.query此属性是一个对象,其中包含路由中每个查询字符串参数的属性。当查询解析器设置为禁用时,为空对象{},否则为配置的查询解析器的结果。
由于req.query的形状基于用户控制的输入,因此此对象中的所有属性和值都是不受信任的,应该在信任之前进行验证。例如,req.query.foo.toString()可能以多种方式失败,例如foo可能不存在或可能不是字符串,toString可能不是函数而是字符串或其他用户输入。
// GET /search?q=tobi+ferret
console.dir(req.query.q)
// => 'tobi ferret'
// GET /shoes?order=desc&shoe[color]=blue&shoe[type]=converse
console.dir(req.query.order)
// => 'desc'
console.dir(req.query.shoe.color)
// => 'blue'
console.dir(req.query.shoe.type)
// => 'converse'
// GET /shoes?color[]=blue&color[]=black&color[]=red
console.dir(req.query.color)
// => ['blue', 'black', 'red']查看document
https://stackoverflow.com/questions/64696880
复制相似问题