1

I am relatively new to Node.JS and have been reading a lot about the topic. However I am at an impasse. I am trying to get the absolute path of a file:

Example: https://localhost:8080.../public/img/apple.jpg

Instead I am only getting nothing printed back in console or just the usual:

/public/img,apple.jpg

Below are is my JavaScript file I run in node. I have tried a few things, however I believe these are probably the closest.

Note: I have tried a lot inside 'app.get' function but it never seems to print out to the console.

var fs = require('fs');
var http = require("http");
var http = require("http");
var url = require("url");
var req = require('request')

http.createServer(function (request, response) {
response.writeHead(200, {'Content-Type': 'text/plain'});

   var express = require('express');
   var app = express();
   var path = require('path');

   app.get('../img/apple.jp', function(req, res) {
      var dir = req.params.dir;
      console.log(req.originalUrl)

      var pathname = url.parse(req.url).pathname;
      var fullUrl = req.protocol + '://' + req.originalUrl;

      console.log("Request for " + pathname + " received.");
      console.log("test" + fullUrl);

      // res.sendFile(path.join(__dirname + 'sliderImages.json'));
      var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
      console.log(fullUrl);
});

Some of the following links I have went to are as followed:

  1. https://nodejs.org/api/path.html
  2. Get application full path in Node.js
  3. How do I get the path to the current script with Node.js?
  4. How to get the full url in Express?

I have went into others as well but I believe these are the closest from what I have read. Especially number 4.

halfer
  • 19,824
  • 17
  • 99
  • 186
L1ghtk3ira
  • 3,021
  • 6
  • 31
  • 70
  • Did you try `path.resolve`? https://millermedeiros.github.io/mdoc/examples/node_api/doc/path.html#path.resolve – bozdoz May 25 '16 at 04:46

2 Answers2

0
  • You forgot the g in ../img/apple.jp.
  • You have required http twice.
  • You have a req object in your app.get but you use request instead. (be careful with copy/past of other's code)
Martial
  • 1,446
  • 15
  • 27
  • Sorry unfortunetly that's not the problem, I made the fix and still nothing. I will update on here shortly – L1ghtk3ira May 25 '16 at 10:49
  • Alright so I figured out what was the problem I had app.get inside of httpCreateServer. Had to take it out and have on a seperate port. The only problem now is im not sure the path I give it for finding a file in app.get(file – L1ghtk3ira May 25 '16 at 11:54
  • @l1ghtk3ira : maybe you should consider using [express.static()](http://expressjs.com/en/starter/static-files.html) to serve static files. – Martial May 25 '16 at 12:09
0

The problem was (for beginners) that app.get needs to have it's own port. I was trying it inside http.CreateServer because you think you created it now do things inside it. However it seems it requires to be outside and have its own port for making a call.

var fs = require('fs');
var http = require("http");

var express = require('express');
var app = express();
var path = require('path');
var http = require("http");
var url = require("url");
var req = require('request')
http.createServer(function (request, response) {

   response.writeHead(200, {'Content-Type': 'text/plain'});

   contents = fs.readFileSync("sliderImages.json", "utf8");

   response.end(contents);
}).listen(8080);

app.get('sliderImages.json', function(req, res) {
    var dir = req.params.dir;
    console.log(req.originalUrl)
    var pathname = url.parse(req.url).pathname;
    var fullUrl = req.protocol + '://' + req.originalUrl;
    console.log("Request for " + pathname + " received.");
    console.log("test" + fullUrl);

    var fullUrl = req.protocol + '://' + req.get('host') + req.originalUrl;
    console.log(fullUrl);

    res.send(url.parse(req.url).pathname);

});

app.listen(3000);
L1ghtk3ira
  • 3,021
  • 6
  • 31
  • 70