[英]I use routes in my Node.JS app and when I try to open one of the routes from the browser I get an error
That's my app. 那是我的应用。 When I try to open ip/delete I get an error
当我尝试打开ip / delete时出现错误
Cannot GET /delete
无法获取/删除
I'm following this tutorial, https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm 我正在关注本教程https://www.tutorialspoint.com/nodejs/nodejs_express_framework.htm
var express = require('express');
var app = express();
app.use(express.static('public'));
app.get('/', function(req, res) {
console.log("Got a GET request");
res.send('GBArena Labs');
})
app.post('/', function(req, res) {
console.log("Got a POST request");
})
app.delete('/delete', function(req, res) {
res.send('Delete');
console.log("Got a DELTE request");
})
app.listen(8081);
You can't navigate to a DELETE. 您无法导航到DELETE。 You have to send a DELETE request.
您必须发送DELETE请求。 In example :
例如:
<a href="/delete">My delete link<a/>
will be sent as a GET request and not a DELETE. <a href="/delete">My delete link<a/>
将作为GET请求而不是DELETE发送。
In order to hit your delete endpoint, you would have to use a ajax library like jquery $.ajax and using 为了击中删除端点,您必须使用ajax库(例如jquery $ .ajax)并使用
$.ajax({
url: '/delete',
type: 'DELETE',
success: function(result) {
// Do something with the result
}
});
from the client side. 从客户端。
There are many ways, but i would suggest you looking into : HTTP verbs 有很多方法,但是我建议您研究一下: HTTP动词
您可能需要在没有/ delete的情况下调用路由,并使用REST客户端(例如Fiddler或Postman)的HTTP动词DELETE。
First, you try to access by ip/delete
endpoint. 首先,您尝试通过
ip/delete
终结点进行访问。 I assume you will get an error because you are not using the port of the application (you have to access by ip:port/delete) 我假设您会收到错误消息,因为您没有使用应用程序的端口(必须通过ip:port / delete进行访问)
You have to know about the usage of DELETE method. 您必须了解DELETE方法的用法。 When you try to directly access the
ip/delete
endpoint, your browser will set it as GET method while in your application, there is not GET method for ip/delete
当您尝试直接访问
ip/delete
端点时,浏览器会将其设置为GET方法,而在您的应用程序中,没有ip/delete
GET方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.