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:
- https://nodejs.org/api/path.html
- Get application full path in Node.js
- How do I get the path to the current script with Node.js?
- 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.