Javascript 按钮将http发送到NodeJS/Express后端以执行函数
我在前端有一个按钮,在服务器端后端使用nodejs和express。我在后端有一个函数(基本上控制Philips Hue API),我希望它在通过http请求单击按钮时执行 我尝试过不同的方法。当我提取Philips Hue控件并在git bash中运行它时,它的后端脚本可以独立工作。我认为最终会有一些概念或编码错误 Html按钮Javascript 按钮将http发送到NodeJS/Express后端以执行函数,javascript,html,node.js,http,express,Javascript,Html,Node.js,Http,Express,我在前端有一个按钮,在服务器端后端使用nodejs和express。我在后端有一个函数(基本上控制Philips Hue API),我希望它在通过http请求单击按钮时执行 我尝试过不同的方法。当我提取Philips Hue控件并在git bash中运行它时,它的后端脚本可以独立工作。我认为最终会有一些概念或编码错误 Html按钮 <button id="pulse" type="button" class="btn btn-danger">Pulsing Lights</bu
<button id="pulse" type="button" class="btn btn-danger">Pulsing Lights</button>
后端/服务器端JS
const port = 3000;
const server = http.Server(app);
server.listen(process.env.PORT || 3000, function(){
console.log('Server running on port ' + port);
});
const app = express();
pulseLight = lightState.create().on().colorLoop();
function setPulseLight() {
nodeHueapi.setLightState(1, pulseLight, function (err, lights) {
if (err) throw err;
displayResult(lights);
});
nodeHueapi.setLightState(2, pulseLight, function (err, lights) {
if (err) throw err;
displayResult(lights);
});
nodeHueapi.setLightState(3, pulseLight, function (err, lights) {
if (err) throw err;
displayResult(lights);
});
}
app.post('/huePulseLight', function(req, res){
console.log("Pulse Light Set");
setPulseLight();
});
您缺少服务器上的应用程序。侦听(端口)
此外,您没有将任何内容从服务器发送回客户端,这可能会导致客户端保持与服务器的连接打开,并且您的获取承诺将永远无法解决。您的服务器上缺少
应用程序。侦听(端口)
此外,您没有将任何内容从服务器发送回客户端,这可能会导致客户端保持与服务器的连接打开,并且您的获取承诺将永远无法解决。隔离问题。在添加任何其他内容之前,请确保服务器和浏览器控制台通信正常。这或多或少是客户端和服务器通信的最低代码。在
test
中运行node server.js
,导航到localhost:3000
,单击文本,观察控制台输出
test/server.js
const express = require("express")
const app = express()
// make index.html accessible to clients
app.use(express.static('public'))
app.post('/huePulseLight', function(request, response){
console.log("Pulse Light Set");
response.send("Click Recorded")
});
app.listen(3000)
test/public/index.html
foo
const pulseButton=document.getElementById(“脉冲”)
pulseButton.addEventListener('click',function(){
获取('/huePulseLight',{method:'POST'})
.then(response=>response.text())
.then(text=>console.log(text))
})
隔离问题。在添加任何其他内容之前,请确保服务器和浏览器控制台通信正常。这或多或少是客户端和服务器通信的最低代码。在test
中运行node server.js
,导航到localhost:3000
,单击文本,观察控制台输出
test/server.js
const express = require("express")
const app = express()
// make index.html accessible to clients
app.use(express.static('public'))
app.post('/huePulseLight', function(request, response){
console.log("Pulse Light Set");
response.send("Click Recorded")
});
app.listen(3000)
test/public/index.html
foo
const pulseButton=document.getElementById(“脉冲”)
pulseButton.addEventListener('click',function(){
获取('/huePulseLight',{method:'POST'})
.then(response=>response.text())
.then(text=>console.log(text))
})
谢谢,我前面有端口配置。只是不想洪水泛滥。修改以反映。如果您在端口3000的本地计算机上运行服务器,您是否尝试将fetch URI参数更改为localhost:3000/huePulseLight
谢谢,我前面有端口配置。只是不想洪水泛滥。修改以反映。如果您在端口3000上的本地计算机上运行服务器,是否尝试将fetch URI参数更改为localhost:3000/huePulseLight
no,则不会。我认为http请求做得不正确,希望得到任何帮助。不,没有。我认为http请求做得不正确,希望您能提供帮助。当您使用postman时,api有任何响应吗?没有。不过我认为端点是可以的。我怀疑问题在于http调用。当您使用postman时,api有任何响应吗?没有。不过我认为端点是可以的。我怀疑问题在于http调用。