简体   繁体   中英

Get absolute URL path using Node Js Express and possibly ajax

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.

  • 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)

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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