例如,我有以下URL:http://localhost/chat.html?channel=talk
如何在Node.js中获取参数channel
的值?
我想将channel
的值存储在变量中。
我将server.get更改为:
server.get("/channel", (req, res) => {
let query = url.parse(req.url, true).query;
console.log(req.query.channel);
let rueckgabe = {
channel: req.query.channel
};
res.send(JSON.stringify(rueckgabe));
});
现在,我期望控制台上输出channel
的值,但是什么也没有出现。
这是index.js
的完整代码:
//Server erstellen
const express = require("express");
let server = express();
server.use(express.static("public"));
//Socket.io
const http = require("http");
let httpServer = http.Server(server);
const socketIo = require("socket.io");
let io = socketIo(httpServer);
//Eventlistener bei Verbindungsaufbau
io.on("connection", (socket) => {
console.log(socket.id);
socket.on("chatnachricht", eingabe => {
io.emit("nachricht", eingabe);
});
});
let stdIn = process.openStdin();
stdIn.addListener("data", (eingabe) => {
io.emit("nachricht", eingabe.toString());
});
server.get("/channel", (req, res) => {
let query = url.parse(req.url, true).query;
console.log(query);
let rueckgabe = {
channel: query.channel
};
//res.send(JSON.stringify(rueckgabe));
res.send(JSON.stringify(rueckgabe));
});
httpServer.listen(80, () => {
console.log("Server läuft");
});
到目前为止,此代码有效,但有局限性:
//Server erstellen
const express = require("express");
let server = express();
server.use(express.static("public"));
const http = require("http");
let httpServer = http.Server(server);
const socketIo = require("socket.io");
let io = socketIo(httpServer);
var router = express.Router();
const url = require("url");
var path = require('path');
//Eventlistener bei Verbindungsaufbau
io.on("connection", (socket) => {
console.log(socket.id);
socket.on("chatnachricht", eingabe => {
io.emit("nachricht", eingabe);
});
});
/*
let stdIn = process.openStdin();
stdIn.addListener("data", (eingabe) => {
io.emit("nachricht", eingabe.toString());
});
*/
server.get("/chat", (req, res) => {
let query = url.parse(req.url, true).query;
console.log(query.channel);
let rueckgabe = {
channel: query.channel
};
res.sendFile('chat.html', { root: path.join(__dirname, 'public/') });
//res.send(JSON.stringify(rueckgabe));
});
httpServer.listen(80, () => {
console.log("Server läuft");
});
现在它可以与server.get()
一起使用,但是我不能同时使用res.sendFile('chat.html', { root: path.join(__dirname, 'public/') });
和res.send(JSON.stringify(rueckgabe));
。如何同时使用两者?
答案 0 :(得分:1)
您似乎正在使用Express框架的Node。
From the docs,查询字符串参数可以通过req.query
访问:
server.get("/channel", (req, res) => {
let id = req.query.id; // where "id" is a paramter on the query string
}
如果您需要请求的完整网址:
server.get("/channel", (req, res) => {
let fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
}
答案 1 :(得分:0)
您为该网址http://localhost/chat.html?channel=talk
提到的好消息是,您在服务器中看不到channel参数。那是因为您没有达到已定义的端点。
从上方复制代码
server.get("/channel", (req, res) => {
let query = url.parse(req.url, true).query;
console.log(req.query.channel);
let rueckgabe = {
channel: req.query.channel
};
res.send(JSON.stringify(rueckgabe));
});
您在此处设置/channel
网址。使用此配置,如果要获取查询参数,则需要调用http://localhost:{portnumber}/channel?channel=somerandomvalue
如果要使用/chat
网址,请按以下方式更改配置:
server.get("/chat", (req, res) => {
let query = url.parse(req.url, true).query;
console.log(req.query.channel);
let rueckgabe = {
channel: req.query.channel
};
res.send(JSON.stringify(rueckgabe));
});
并致电http://localhost:{portnumber}/chat?channel=somerandomvalue
如果您想在将url名称用作相同文件名的同时提供静态html,则可以执行以下操作:
var router = express.Router();
var path = require('path');
router.get('/chat', function(req, res) {
// where chat.html is in the public directory
console.log(req.query.channel);
res.sendFile('chat.html', { root: path.join(__dirname, '../public/') });
});