How to use XHR API in Node.js?

前端 未结 2 2186
礼貌的吻别
礼貌的吻别 2021-02-12 19:43

This is a follow-up to my previous question

Suppose I\'ve some javascript code, which runs fine on client (in a browser). This code makes a lot of XHR

2条回答
  •  梦谈多话
    2021-02-12 20:17

    Natively Node.js does not provide the browser XHR API. There is, however, a node module xmlhttprequest that does.

    If the file is on the server itself, you can use the fs.readFile or fs.readFileSync.

    If it is on a remote server, then you can do an asynchronous XHR type request using a module like request: https://www.npmjs.com/package/request. This requires some rewriting of code.

    Probably the least re-writing of your client-side code will be if you use the xmlhttprequest node module. It implements the browser XHR API for node.

提交回复
热议问题