Node.js: get path from the request

Nabil Djarallah

I have a service called "localhost:3000/returnStat" that should take a file path as parameter. For example '/BackupFolder/toto/tata/titi/myfile.txt'.

How can I test this service on my browser? How can I format this request using Express for instance?

exports.returnStat = function(req, res) {

var fs = require('fs');
var neededstats = [];
var p = __dirname + '/' + req.params.filepath;

fs.stat(p, function(err, stats) {
    if (err) {
        throw err;
    }
    neededstats.push(stats.mtime);
    neededstats.push(stats.size);
    res.send(neededstats);
});
};
Gaurav
var http = require('http');
var url  = require('url');
var fs   = require('fs');

var neededstats = [];

http.createServer(function(req, res) {
    if (req.url == '/index.html' || req.url == '/') {
        fs.readFile('./index.html', function(err, data) {
            res.end(data);
        });
    } else {
        var p = __dirname + '/' + req.params.filepath;
        fs.stat(p, function(err, stats) {
            if (err) {
                throw err;
            }
            neededstats.push(stats.mtime);
            neededstats.push(stats.size);
            res.send(neededstats);
        });
    }
}).listen(8080, '0.0.0.0');
console.log('Server running.');

I have not tested your code but other things works

If you want to get the path info from request url

 var url_parts = url.parse(req.url);
 console.log(url_parts);
 console.log(url_parts.pathname);

1.If you are getting the URL parameters still not able to read the file just correct your file path in my example. If you place index.html in same directory as server code it would work...

2.if you have big folder structure that you want to host using node then I would advise you to use some framework like expressjs

If you want raw solution to file path

var http = require("http");
var url = require("url");

function start() {
function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
}

http.createServer(onRequest).listen(8888);
console.log("Server has started.");
}

exports.start = start;

source : http://www.nodebeginner.org/

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Node js Get folder path from a file

From Dev

how to get the value from request.get in node js

From Dev

Get data from GET request in node.js parameters undefined

From Dev

How to get cookies from request module in node.js?

From Dev

Save GET request return from node.js and mongoose to variable

From Dev

How to get a synchronuous result from an HTTP request in node.js

From Dev

Node.js get request from MongoDB returning undefined

From Dev

Node js request get page error 500

From Dev

Simple get request with node.js and express

From Dev

Node.js https get request ECONNRESET

From Dev

use GET method in node js request?

From Dev

Get request in openshift node js app

From Dev

Chaining GET request with a response Node.js

From Dev

HTTP GET request with parameters Node.js

From Dev

get request object array in node.js

From Dev

Perform Get request with body - Node js

From Dev

Get DOM path from a node to another

From Dev

How to get the full path of an executable which is in $PATH in node.js?

From Dev

How to get the full path of an executable which is in $PATH in node.js?

From Dev

Get values from request.body in Node

From Dev

Get request body from node.js's http.IncomingMessage

From Dev

undefined data using http.get() request to node.js from angular2?

From Dev

Parse POST request from Node js + handlebars

From Dev

Parse POST request from Node js + handlebars

From Dev

Json Request from Node js to Django

From Dev

Node js using data returned from request/request api call

From Dev

How can i wait for data until it get received in Node.js from a TCP server while GET request is sent by the user?

From Java

How to get data out of a Node.js http get request

From Dev

Get Request Header in Node.js to get PHP Session ID

Related Related

  1. 1

    Node js Get folder path from a file

  2. 2

    how to get the value from request.get in node js

  3. 3

    Get data from GET request in node.js parameters undefined

  4. 4

    How to get cookies from request module in node.js?

  5. 5

    Save GET request return from node.js and mongoose to variable

  6. 6

    How to get a synchronuous result from an HTTP request in node.js

  7. 7

    Node.js get request from MongoDB returning undefined

  8. 8

    Node js request get page error 500

  9. 9

    Simple get request with node.js and express

  10. 10

    Node.js https get request ECONNRESET

  11. 11

    use GET method in node js request?

  12. 12

    Get request in openshift node js app

  13. 13

    Chaining GET request with a response Node.js

  14. 14

    HTTP GET request with parameters Node.js

  15. 15

    get request object array in node.js

  16. 16

    Perform Get request with body - Node js

  17. 17

    Get DOM path from a node to another

  18. 18

    How to get the full path of an executable which is in $PATH in node.js?

  19. 19

    How to get the full path of an executable which is in $PATH in node.js?

  20. 20

    Get values from request.body in Node

  21. 21

    Get request body from node.js's http.IncomingMessage

  22. 22

    undefined data using http.get() request to node.js from angular2?

  23. 23

    Parse POST request from Node js + handlebars

  24. 24

    Parse POST request from Node js + handlebars

  25. 25

    Json Request from Node js to Django

  26. 26

    Node js using data returned from request/request api call

  27. 27

    How can i wait for data until it get received in Node.js from a TCP server while GET request is sent by the user?

  28. 28

    How to get data out of a Node.js http get request

  29. 29

    Get Request Header in Node.js to get PHP Session ID

HotTag

Archive