nodejs http.createServer read url and remove urlprefix
up vote
0
down vote
favorite
Here is what i want:
I have an url "10.0.0.1/0000000/abctest/filedirectory/filename",
and with this url, I can read the file accordingly
Thus, i create a server with "http.createServer" and binded at 8001 port, and set nginx that whenever it read /0000000/abctest/, it will redirect to port 8001
After i added the nginx reverse proxy, I try to write my server code, and here is my http server code
var port = 8001;
var http = require('http');
var fs = require('fs');
var DefaultFileDir = '/home/ubuntu/Server/Proj/static';
var UrlPrefix = "/0000000/abctest"
var hostname = "127.0.0.1"
//var host = hostname+UrlPrefix
var server= http.createServer(function(req,res){
var url = req.url;
console.log(url);
var file = DefaultFileDir + url;
fs.readFile(file, function(err,data){
if(err){
console.log(err);
res.writeHeader(404,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write('<h1>404</h1>');
res.end();
}else{
if(url.substr(url.length-3, 3) == ".gz"){
res.writeHeader(200,{
'Content-Encoding' : 'gzip',
'content-type' : 'application/octet-stream'
});
}else{
res.writeHeader(200,{
'content-type' : 'text/html;charset="utf-8"'
});
}
res.write(data);
res.end();
}
});
}).listen(port,hostname);
console.log("Static file server running atn => http://localhost:" + port + "/nCTRL + C to shutdown");
And the problem come out:
it dont know how to ignore the url prefix "/0000000/abctest"
Error: ENOENT: no such file or directory, open '/home/ubuntu/xxxxx/static/0000000/abctest/folder001/index.html'
However my actual file path shall be '/home/ubuntu/xxxxx/static/folder001/index.html'
is that http.createserver cannot ignore the url prefix? May i seek for some hints from that.
node.js nginx reverse-proxy
add a comment |
up vote
0
down vote
favorite
Here is what i want:
I have an url "10.0.0.1/0000000/abctest/filedirectory/filename",
and with this url, I can read the file accordingly
Thus, i create a server with "http.createServer" and binded at 8001 port, and set nginx that whenever it read /0000000/abctest/, it will redirect to port 8001
After i added the nginx reverse proxy, I try to write my server code, and here is my http server code
var port = 8001;
var http = require('http');
var fs = require('fs');
var DefaultFileDir = '/home/ubuntu/Server/Proj/static';
var UrlPrefix = "/0000000/abctest"
var hostname = "127.0.0.1"
//var host = hostname+UrlPrefix
var server= http.createServer(function(req,res){
var url = req.url;
console.log(url);
var file = DefaultFileDir + url;
fs.readFile(file, function(err,data){
if(err){
console.log(err);
res.writeHeader(404,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write('<h1>404</h1>');
res.end();
}else{
if(url.substr(url.length-3, 3) == ".gz"){
res.writeHeader(200,{
'Content-Encoding' : 'gzip',
'content-type' : 'application/octet-stream'
});
}else{
res.writeHeader(200,{
'content-type' : 'text/html;charset="utf-8"'
});
}
res.write(data);
res.end();
}
});
}).listen(port,hostname);
console.log("Static file server running atn => http://localhost:" + port + "/nCTRL + C to shutdown");
And the problem come out:
it dont know how to ignore the url prefix "/0000000/abctest"
Error: ENOENT: no such file or directory, open '/home/ubuntu/xxxxx/static/0000000/abctest/folder001/index.html'
However my actual file path shall be '/home/ubuntu/xxxxx/static/folder001/index.html'
is that http.createserver cannot ignore the url prefix? May i seek for some hints from that.
node.js nginx reverse-proxy
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
Here is what i want:
I have an url "10.0.0.1/0000000/abctest/filedirectory/filename",
and with this url, I can read the file accordingly
Thus, i create a server with "http.createServer" and binded at 8001 port, and set nginx that whenever it read /0000000/abctest/, it will redirect to port 8001
After i added the nginx reverse proxy, I try to write my server code, and here is my http server code
var port = 8001;
var http = require('http');
var fs = require('fs');
var DefaultFileDir = '/home/ubuntu/Server/Proj/static';
var UrlPrefix = "/0000000/abctest"
var hostname = "127.0.0.1"
//var host = hostname+UrlPrefix
var server= http.createServer(function(req,res){
var url = req.url;
console.log(url);
var file = DefaultFileDir + url;
fs.readFile(file, function(err,data){
if(err){
console.log(err);
res.writeHeader(404,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write('<h1>404</h1>');
res.end();
}else{
if(url.substr(url.length-3, 3) == ".gz"){
res.writeHeader(200,{
'Content-Encoding' : 'gzip',
'content-type' : 'application/octet-stream'
});
}else{
res.writeHeader(200,{
'content-type' : 'text/html;charset="utf-8"'
});
}
res.write(data);
res.end();
}
});
}).listen(port,hostname);
console.log("Static file server running atn => http://localhost:" + port + "/nCTRL + C to shutdown");
And the problem come out:
it dont know how to ignore the url prefix "/0000000/abctest"
Error: ENOENT: no such file or directory, open '/home/ubuntu/xxxxx/static/0000000/abctest/folder001/index.html'
However my actual file path shall be '/home/ubuntu/xxxxx/static/folder001/index.html'
is that http.createserver cannot ignore the url prefix? May i seek for some hints from that.
node.js nginx reverse-proxy
Here is what i want:
I have an url "10.0.0.1/0000000/abctest/filedirectory/filename",
and with this url, I can read the file accordingly
Thus, i create a server with "http.createServer" and binded at 8001 port, and set nginx that whenever it read /0000000/abctest/, it will redirect to port 8001
After i added the nginx reverse proxy, I try to write my server code, and here is my http server code
var port = 8001;
var http = require('http');
var fs = require('fs');
var DefaultFileDir = '/home/ubuntu/Server/Proj/static';
var UrlPrefix = "/0000000/abctest"
var hostname = "127.0.0.1"
//var host = hostname+UrlPrefix
var server= http.createServer(function(req,res){
var url = req.url;
console.log(url);
var file = DefaultFileDir + url;
fs.readFile(file, function(err,data){
if(err){
console.log(err);
res.writeHeader(404,{
'content-type' : 'text/html;charset="utf-8"'
});
res.write('<h1>404</h1>');
res.end();
}else{
if(url.substr(url.length-3, 3) == ".gz"){
res.writeHeader(200,{
'Content-Encoding' : 'gzip',
'content-type' : 'application/octet-stream'
});
}else{
res.writeHeader(200,{
'content-type' : 'text/html;charset="utf-8"'
});
}
res.write(data);
res.end();
}
});
}).listen(port,hostname);
console.log("Static file server running atn => http://localhost:" + port + "/nCTRL + C to shutdown");
And the problem come out:
it dont know how to ignore the url prefix "/0000000/abctest"
Error: ENOENT: no such file or directory, open '/home/ubuntu/xxxxx/static/0000000/abctest/folder001/index.html'
However my actual file path shall be '/home/ubuntu/xxxxx/static/folder001/index.html'
is that http.createserver cannot ignore the url prefix? May i seek for some hints from that.
node.js nginx reverse-proxy
node.js nginx reverse-proxy
edited Nov 22 at 5:29
AIqbalRaj
9992518
9992518
asked Nov 22 at 3:34
Question-er XDD
116212
116212
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423508%2fnodejs-http-createserver-read-url-and-remove-urlprefix%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown