Hene
Hene

Reputation: 159

Node.js post request local file

I'm writing an server script with node.js which includes an request. 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

Upvotes: 0

Views: 3897

Answers (1)

Andrew L
Andrew L

Reputation: 5486

No you cannot. Request states in their docs the url/uri must be fully qualified

uri || url - fully qualified uri or a parsed url object from url.parse()

Here is an answer describing a fully qualified URI

An alternative to read a local file is reading it using fs.readFile. Here is an example

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

Upvotes: 1

Related Questions