简体   繁体   English

Node.js发布请求本地文件

[英]Node.js post request local file

I'm writing an server script with node.js which includes an request. 我正在用node.js编写一个包含请求的服务器脚本。 I'm wondering if you could send an post request to local file like: 我想知道您是否可以将发布请求发送到本地文件,例如:

var request = require('request');
request({
    method: 'POST',
    url: '/v1/functions.php',
    formData: {
        'method': 'validate',
        'id': message.id,
        'token': message.token
    }
}, function(error, response, body) {
    if (error) {
        console.log(error);
    }

    console.log('response: '+ response.statusCode);
    console.log('body: '+ body);
});

But this is throwing an error: 但这会引发错误:

Error: Invalid URI "/v1/functions.php"

It works if I use the full path: 如果我使用完整路径,它将起作用:

http://localhost/something/something/v1/functions.php

No you cannot. 你不能。 Request states in their docs the url/uri must be fully qualified 在其文档中请求状态url / uri必须完全合格

uri || uri || url - fully qualified uri or a parsed url object from url.parse() url-完全限定的uri或从url.parse()解析的url对象

Here is an answer describing a fully qualified URI 这是描述完全限定URI的答案

An alternative to read a local file is reading it using fs.readFile . 读取本地文件的另一种方法是使用fs.readFile读取它。 Here is an example 这是一个例子

var fs = require('fs');
fs.readFile('/functions.php', (err, data) => {
  if (err) throw err;
  console.log(data);
});

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM