-1

In below code i can able to listen port 9080 but i need to access my local file path (D:\xampp\htdocs\ln\try\index.html) using http request

var http = require('http');

const PORT=9080; 

function handleRequest(request, response){
    response.end('It Works!! Path Hit: ' + request.url);
}

var server = http.createServer(handleRequest);

  server.listen(PORT, function(){
    //Callback triggered when server is successfully listening. Hurray!
    console.log("Server listening on: http://localhost:%s", PORT);
});
Pavan
  • 665
  • 5
  • 16
  • are you after __filename? – tmcgoo Jun 02 '15 at 16:37
  • All i need is to access my local path files using server with nodejs – Pavan Jun 02 '15 at 16:38
  • 1
    You need to be more specific. Do you want to require files into your index.html? Do you want to read files into your app or do you just want to actual path? __filename may be what your after or I may be misunderstanding your question. https://nodejs.org/api/globals.html#globals_filename – tmcgoo Jun 02 '15 at 16:47
  • i want to access path like this http://localhost/pp/login.html – Pavan Jun 02 '15 at 16:50
  • i want to run my html files as http://localhost:9080/example/index.html request instead of D:example/index.html – Pavan Jun 02 '15 at 16:51
  • Currently i am opening file as this file:///D:/xampp/htdocs/ln/try/index.html where as i want to open as httpL//localhost:9080/index.html – Pavan Jun 02 '15 at 16:54

2 Answers2

0

installing http-server using node as ( npm install http-server -g ). When you add globally this node module you can run server from any where in your path.

Run server any where in the computer as below shown image

Pavan
  • 665
  • 5
  • 16
-2

How to serve a html file with node... Loading basic HTML in Node.js

You read the html file in with the fs module then create the http server, on requests you write the html to the response and then end the response.

Its much easier though if you get express. Render basic HTML view?

Community
  • 1
  • 1
tmcgoo
  • 122
  • 6
  • i am not asking for only html file any path in server. i think this helps you understand – Pavan Jun 03 '15 at 07:42