简体   繁体   English

我在Node.JS应用程序中使用路由,当我尝试从浏览器中打开其中一个路由时,出现错误

[英]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.

相关问题 Node.js:组织我的应用程序:在单独的文件中使用路由和模型,如何从路由中获取模型? - Node.js : organising my application : using routes and models in separate files, how can I get models from routes? 使用sendFile()方法在node.js中为两条路由提供静态文件时,为什么会出错? - Why do I get an error when I serve a static file for two routes in node.js using sendFile() method 当我尝试在 Visual Studio Code 的终端中使用 Node.js 时,我收到“文档未定义”错误消息 - When I try to use Node.js in Visual Studio Code’s terminal, I get “document is not defined” error messages 我的服务器已启动,但是当我尝试连接它时,出现错误[Node.js] - My Server is up but when I try to connect to it I am getting an error [Node.js] 我如何使用外部路由.js所以我不必在app.js中定义我的路由? - How do I use external routes.js so I don't have to define my routes in app.js? 我如何使用角js路由为节点js应用程序 - how do i use angular js routes for node js application 当我尝试在反应中使用路线广告路线时,我的页面不断消失 - my page keeps disappearing when I try to use routes ad route in reacts 当我尝试通过 node.js 从 mongodb 获取数据时出现问题? - There is some issue when I try to get data from mongodb by node.js? 我想让我的应用程序运行得更好,将它从浏览器移动到node.js提高性能? - I'd like to make my app run better, will moving it from browser to node.js improve performance? 我应该什么时候开始和结束我的 node.js SQL 连接,我应该只使用来自 get go 的池吗? - When am I supposed to start and end my node.js SQL connection and should I just use pooling from the get go?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM