Node.js: get path from the request

var http = require(‘http’); var url = require(‘url’); var fs = require(‘fs’); var neededstats = []; http.createServer(function(req, res) { if (req.url == ‘/index.html’ || req.url == “https://stackoverflow.com/”) { fs.readFile(‘./index.html’, function(err, data) { res.end(data); }); } else { var p = __dirname + “https://stackoverflow.com/” + req.params.filepath; fs.stat(p, function(err, stats) { if (err) { throw err; } … Read more

CSS file blocked: MIME type mismatch (X-Content-Type-Options: nosniff)

I just ran into the same issue. It appears to be a quirk of Express that can manifest itself for a few different reasons, judging by the number of hits from searching the web for “nodejs express css mime type”. Despite the type=”text/css” attribute we put in our <link elements, Express is returning the CSS … Read more

MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]

You have to install MongoDB database server first in your system and start it. Use the below link to install MongoDB https://docs.mongodb.com/manual/installation/ If you have installed MongoDB check if the server is in which state (start/stop). Try to connect through mongo shell client.

What is the difference between res.send and res.write in express?

res.send res.send is only in Express.js. Performs many useful tasks for simple non-streaming responses. Ability to automatically assigns the Content-Length HTTP response header field. Ability to provides automatic HEAD & HTTP cache freshness support. Practical explanation res.send can only be called once, since it is equivalent to res.write + res.end() Example: app.get(‘/user/:id’, function (req, res) … Read more

Why is PassportJS in Node not removing session on logout

Brice’s answer is great, but I still noticed an important distinction to make; the Passport guide suggests using .logout() (also aliased as .logOut()) as such: app.get(‘/logout’, function(req, res){ req.logout(); res.redirect(“https://stackoverflow.com/”); //Can fire before session is destroyed? }); But as mentioned above, this is unreliable. I found it behaved as expected when implementing Brice’s suggestion like … Read more

How to use query parameters in Nest.js?

Query parameters You have to remove :params for it to work as expected: @Get(‘findByFilter’) async findByFilter(@Query() query): Promise<Article[]> { // … } Path parameters The :param syntax is for path parameters and matches any string on a path: @Get(‘products/:id’) getProduct(@Param(‘id’) id) { matches the routes localhost:3000/products/1 localhost:3000/products/2abc // … Route wildcards To match multiple endpoints … Read more

Error: Failed to lookup view in Express

Adding to @mihai’s answer: If you are in Windows, then just concatenating __dirname’ + ‘../public’ will result in wrong directory name (For example: c:\dev\app\module../public). Instead use path, which will work irrespective of the OS: var path = require (‘path’); app.use(express.static(path.join(__dirname + ‘../public’))); path.join will normalize the path separator character and will return correct path value.

Error: getaddrinfo ENOTFOUND in nodejs for get call

getaddrinfo ENOTFOUND means client was not able to connect to given address. Please try specifying host without http: var optionsget = { host : ‘localhost’, port : 3010, path : ‘/quote/random’, // the rest of the url with parameters if needed method : ‘GET’ // do GET }; Regarding learning resources, you won’t go wrong … Read more