express
Angular and Express routing
Add these routes to your express server app.get(‘/partials/:filename’, routes.partials); app.use(routes.index); Then in routes.js exports.partials = function(req, res){ var filename = req.params.filename; if(!filename) return; // might want to change this res.render(“partials/” + filename ); }; exports.index = function(req, res){ res.render(‘index’, {message:”Hello!!!”}); }; This will make sure that express returns rendered templates when making requests to partials/index … Read more
How do I detect whether I am on server on client in next.js?
Now (2020 Jan) it should be typeof window === ‘undefined’ since process.browser is deprecated Refer to https://github.com/zeit/next.js/issues/5354#issuecomment-520305040
Does Axios support Set-Cookie? Is it possible to authenticate through Axios HTTP request?
Try this out! axios.get(‘your_url’, {withCredentials: true}); //for GET axios.post(‘your_url’, data, {withCredentials: true}); //for POST axios.put(‘your_url’, data, {withCredentials: true}); //for PUT axios.delete(‘your_url’, data, {withCredentials: true}); //for DELETE For more information on this from the axios docs: “withCredentials indicates whether or not cross-site Access-Control requests should be made using credentials” – https://github.com/axios/axios More detail on withCredentials: https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials
Express routes parameters
Basically, your declared routes are documented in the Express documentation. The second route is resolved by a URL like /api/choice/hello where ‘hello’ is mapped into the req object object as: router.get(‘/api/choice/:id’, function (req, res) { console.log(“choice id is ” + req.params.id); }); What you are actually trying is mapping query parameters. A URL like /api/choice/?id=1 … Read more
Enabling CORS in Create React App utility
If you are needing this for development and wanting to access an api from your react app but getting an error like this- Failed to load http://localhost:8180/tables: The ‘Access-Control-Allow-Origin’ header has a value ‘http://localhost:8180’ that is not equal to the supplied origin. Origin ‘http://localhost:3000’ is therefore not allowed access. Have the server send the header … Read more
How to use passport with express and socket.io?
Here is a solution using Socket.IO 1.0 and Express 4.0. It is similar in spirit to Patrick’s answer. The trick is that since Socket.IO 1.0 has a new middleware API, we can wrap Express’s middleware and put it into the Socket.IO pipeline without delving into the low-level implementation of the session stores. // Set up … Read more
Queries hang when using mongoose.createConnection() vs mongoose.connect()
Unfortunately, this isn’t a simple refactor. 1) .createConnection vs .connect When using .createConnection, you access models via the explicit connection you create with this call. This means that instead of User = mongoose.model(…) you need User = db.model(…). Examples (one, two, three, four) show this isn’t complicated but the change is subtle enough that many … Read more
Request header field Access-Control-Allow-Headers is not allowed by itself in preflight response
When you start playing around with custom request headers you will get a CORS preflight. This is a request that uses the HTTP OPTIONS verb and includes several headers, one of which being Access-Control-Request-Headers listing the headers the client wants to include in the request. You need to reply to that CORS preflight with the … Read more