[英]node.js] how to catch GET in server.js?
I want to run function in server, then use $.ajax
in website.js
我想在服务器中运行 function,然后在
website.js
中使用$.ajax
First, i want to test ajax and cathc GET in server.首先,我想在服务器中测试 ajax 和 cathc GET。
I'll try to test the function that displays "hellooooo" on the console by simply catching GET.我将尝试通过简单地捕获 GET 来测试在控制台上显示“hellooooo”的 function。
However, the code is not working properly.但是,代码无法正常工作。
this is code in server js
这是
server js
中的代码
## server.js ##
const express = require('express');
const http = require('http');
const WebSocket = require('ws');
const path = require('path');
const EventHubReader = require('./scripts/event-hub-reader.js');
const SendMessage = require('./public/js/C2D.js');
const iotHubConnectionString = process.env.IotHubConnectionString;
if (!iotHubConnectionString) {
console.error(`Environment variable IotHubConnectionString must be specified.`);
return;
}
console.log(`Using IoT Hub connection string [${iotHubConnectionString}]`);
const eventHubConsumerGroup = process.env.EventHubConsumerGroup;
console.log(eventHubConsumerGroup);
if (!eventHubConsumerGroup) {
console.error(`Environment variable EventHubConsumerGroup must be specified.`);
return;
}
console.log(`Using event hub consumer group [${eventHubConsumerGroup}]`);
// Redirect requests to the public subdirectory to the root
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.use((req, res /* , next */) => {
res.redirect('/');
});
app.get('./send/', function(req,res) {
console.log("HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
});
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
wss.broadcast = (data) => {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
try {
console.log(`Broadcasting data ${data}`);
client.send(data);
} catch (e) {
console.error(e);
}
}
});
};
server.listen(process.env.PORT || '3000', () => {
console.log('Listening on %d.', server.address().port);
});
const eventHubReader = new EventHubReader(iotHubConnectionString, eventHubConsumerGroup);
(async () => {
await eventHubReader.startReadMessage((message, date, deviceId) => {
try {
const payload = {
IotData: message,
MessageDate: date || Date.now().toISOString(),
DeviceId: deviceId,
};
wss.broadcast(JSON.stringify(payload));
} catch (err) {
console.error('Error broadcasting: [%s] from [%s].', err, message);
}
});
})().catch();
i can found GET log in my application console我可以在我的应用程序控制台中找到 GET 日志
console message
2020-05-15 08:43:23 test GET /send/
but i can't find result of this code但我找不到这段代码的结果
app.get('./send/', function(req,res) {
console.log("HELLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
});
what is problem in my code, and how can i modify to run correctly?我的代码有什么问题,我该如何修改才能正确运行?
I don't think there is supposed to be a '.'我不认为应该有一个'。 dot in
./send/
. ./send/
中的点。 Maybe it should just be /send
也许它应该只是
/send
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.