last day (15 days later) » 

6:12 PM
0
A: Setting up URL base for JSON with expressjs

Krzysztof Sztompkacheck something like this: app.get('/car/models/:filename', function(req, res){ var filename = req.params.filename; var fileDir = __dirname + '/datasource/' + filename; res.download(fileDir); }) then this http://localhost:3000/car/models/carmodel.json should work. I dont test it, youst w...

 
HGB
Thanks but it does not work. My local server does not seem to find the "datasource" directory as it lives outside of my static folder (public). I can't even browser to localhost:3000/datasource/carmodel.json as it cannot find the file. I just want a way to map Express to this directory and mount it to /car/models/. Any ideas?
 
of course you cant get localhost:3000/datasource/carmodel.json. From express you can only get files from static directory or from routes. My solution show how to set route and then get file 'by server' and push it to browser. In this solution you can get all files to which your script has permission. I checked my code and it download file. when you run server and go to localhost:3000/car/models/carmodel.json in browser what is happening (it should download file or show it in browser)? Maybe you have wrong permission to this file?
in my code i make assumption that datasource is in project directory. But looking on your code it may be level up? If yes then change this "var fileDir = __dirname + '/datasource/' + filename;" to this "var fileDir = __dirname + '/../datasource/' + filename;"
 
HGB
It shoud show it in the browser as a static Json. I tried copying it under public/data/carmodel.json and it shows it in the browser fine. It is only a static file located on my machine under the datatsource/ directory in the project so I do not understand the permissions issue. Can't your script just pull the json file and replace its path with car/models/ making it accessible via the localhost:300/car/models/carmodel.json url?
Or it is indeed. I changed the default line " app.use(express.static(__dirname + '/../public'));" to : app.use(express.static('public')); as it was confusing me as well and it still runs under locahost:3000/. That is why I am not understanding how ExpressJS works, too much path confusion.
 
Please update your question and add structure of your project: base dir, public dir, datasource dir, server script, package.json, and script where you set my code.
 
HGB
Hi Kryzystopf I have updated my question, thanks for your patience.
 
6:14 PM
hi, i just saw it.
sorry rof my poor english (it is not my native)
first case, about your structure "__dirname is always the directory in which the currently executing script resides"
 
HGB
No problem at all, I am not English myself.
By __dirname, do you mean that it thinks the directory is actually in /datasource/?
 
HGB
given that the index.js resides in this
So this would have to be: app.get('/car/models/:filename', function(req, res){
var filename = req.params.filename;
var fileDir = filename;
res.download(fileDir);
})
no?
 
by __dirname i mean this variable
check this "var fileDir = __dirname + filename;"
or "var fileDir = __dirname + '/' + filename;"
 
HGB
let me test
 
6:20 PM
ok
 
HGB
No joy
I tested:
then
app.get('/car/models/:filename', function(req, res){
var filename = req.params.filename;
var fileDir = __dirname + filename;
res.download(fileDir);
})
The strange thing is that app.use(express.static('public')); is working under localhost:3000
But it will not find this bloody datasource/ directory
Inside Package.json: "scripts": {
"postinstall": "node_modules/protractor/bin/webdriver-manager update",
"start": "node datasource/index.js",
"e2e": "protractor protractor.conf.js",
"unit": "karma start karma.conf.js",
"server-unit": "mocha test/datasource/**/*.spec.js"
}
 
put it to browser url localhost:300/car/models/carmodel.json
what answer you get?
then put localhost:3000/car/models/dummy
then what answer
first url give port 3000, by mistake i put 300
 
HGB
localhost:3000/car/models/carmodel.json - RESULT: Cannot GET /car/models/carmodel.json
Second one: Cannot GET /car/models/dummy
Very odd.
 
it means that it not find this route
you restarted server?
 
HGB
no, just refreshed.
let me restart
OPHHHAA
Downloading a JSON File!
high five! :-)
 
6:30 PM
refresh browser never work
:)
you always after changes must restar server
 
HGB
I need to REALLY remember that one!
can you just do npm restart?
 
i prefere use nodemon with gulp
 
HGB
Yeah, I also prefer gulp in general but these guys gave me Express which Inever used before
 
they watch my code and auto restart
 
HGB
What is the best way to display the data in the browser instead of downloading?
 
6:32 PM
it is always good to learn new things:)
 
HGB
Too much too learn as it. It feels like a new thing each job/homework. :-)
 
i use res.status(<somestatus>).send(<stringify json>);
 
HGB
what do you mean by <somestatus>?
 
or res.json(<your json>)
when i serve REST API i also set response status
for correct 200 but for validation error 422 with json validation error object
 
HGB
I am not advanced enough to understand that stuff but at least you are pointing me in the right directory.
 
6:36 PM
for REST there are status codes : w3.org/Protocols/rfc2616/rfc2616-sec10.html
of course you could always send 200 (at default all your responses are '200')
 
HGB
Cheers mate, now I need to figure out how to access that from my public/js/app.js file in angular hahaha
 
ok :) please accept my answer :)
 
HGB
Will do!
 
HGB
thanks
 

last day (15 days later) »