简体   繁体   English

为什么我无法访问我的 node.js 基本公共 API?

[英]Why can I not access my node.js basic public API?

Whenever I try to accesse http://localhost:3000/api/courses it doesn't load at all, just hangs.每当我尝试访问http://localhost:3000/api/courses时,它根本不会加载,只是挂起。 I've had a similar problem with React routing not loading any subpages.我在 React 路由没有加载任何子页面时遇到了类似的问题。 I'm not sure what could be the reason.我不确定可能是什么原因。 I am connected via mobile hotspot but I do not connect to the internet using a proxy.我通过移动热点连接,但我没有使用代理连接到互联网。

I've tried different ports with the same result.我尝试了不同的端口,结果相同。

const http = require('http');

const server = http.createServer((req, res) => {
    if (req.url === '/') {
        res.write('Hello World');
        res.end();
    }

    if (req.url === '/api/courses') {
        res.write(JSON.stringify([1, 2, 3]));
        res.end();
    }
});

server.listen(3000);

console.log('Listening on port 3000...');

Expected result is to show me the array [1, 2, 3] .预期的结果是向我展示数组[1, 2, 3] Actual result is that the page just hangs without loading.实际结果是页面只是挂起而没有加载。

No Changes, It is working fine in my pc, Try once in an incognito window or change port.没有更改,它在我的电脑上运行良好,在隐身 window 中尝试一次或更改端口。

const http = require("http");

const server = http.createServer((req, res) => {
  if (req.url === "/") {
    res.write("Hello World");
    res.end();
  }

  if (req.url === "/api/courses") {
    res.write(JSON.stringify([1, 2, 3]));
    res.end();
  }
});

server.listen(8080);

console.log("Listening on port 8080...")

Check link - https://i.stack.imgur.com/R6mLQ.png检查链接 - https://i.stack.imgur.com/R6mLQ.png

Because you are not returning the valid JSON object.因为您没有返回有效的 JSON object。

res.write(JSON.stringify([1, 2, 3]));

Try something like this尝试这样的事情

res.write(JSON.stringify({"result":[1, 2, 3])});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 node.js express 如何访问我的公共文件夹中的文件 - How to access files in my public folder in node.js express 为什么我的 Node.js 网站在公用文件夹中找不到页面? - Why is my Node.js website not finding pages in public folder? 如何构造/访问非node.js应用程序的react组件? - How can I structure/access the react components of my non node.js application? 如何在我的node.js控制台中的javascript文件中声明一个变量? - How can I access a variable declared in a javascript file in my node.js console? 如何访问日期并将日期插入数据库? Node.JS和MongoDB - How can I access and insert dates into my database? Node.JS & MongoDB 为什么我不能通过node.js中的键访问通过socket.io发送的数据? - Why can’t I access data sent via socket.io by key in node.js? 我可以检查我的 API 密钥是否足够隐藏在 node.js 中以推送到 github 吗? - Can I check if my API key is sufficiently hidden in node.js to push to github? 每当在我的 Node.js API 中发出 GET 请求时,我如何发送随机集合? - How can i send a random collection whenever a GET request made in my Node.js API? 为什么我不能使用 node.js 重定向到我的失败页面? - Why can't I redirect to my failure page using node.js? 为什么我不能向我的 Node.js Express 服务器发出 POST 请求 - Why can't I make a POST request to my Node.js Express server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM