Node js routes working only locally - node.js

I need your help concerning routes in node.js. All newly created routes work fine locally, but not on the remote server after pushing changes. Other urls are still working.
Below is the middleware to capture 404 errors:
// All new routes fall here
app.all('*', function (req, res) {
res.status(404).json({
error: 'Not Found'
});
});
I can confirm that I verified all the config files and I don't know why this is happening.
Could someone explain this to me? Maybe I missed something between local and remote?

I found some similar answers here, here and in the oficial Express page here
Basically, the best way to redirect a 404 error is doing something like this:
app.use(function (req, res, next) {
res.status(404).send("Sorry can't find that!")
})
Read more about it in the links at the top.
Hope it help you.

app.use(function (req, res, next) {
var err = new Error('Not Found');
err.status = 404;
next(err);
})
You can use the above code to capture 404 errors.

It's possible if you made changes in server and didn't restart nodejs. If you use PM2 try pm2 restart app_name
You can use pm2 start app.js --watch to let pm2 actively watches for any changes in file and restart for you.
For more info about pm2 - http://pm2.keymetrics.io/docs/usage/quick-start/#usage

use following code. it will work .
app.route('/*').get(function(req, res) {
res.status(404).send("page not found!")
});

Related

Namecheap shared hosting, Express.js, and app.get() gives "cannot GET /path" error

I am following the tutorial here: https://socket.io/get-started/chat/
The code works locally but when I upload/Install it to my Namecheap shared hosting server, I get this error displayed in my browser:
Cannot GET /PathToApp
I have isolated the problem to:
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html"); });
since I have another app that works fine locally but won't work when uploaded to Namecheap.
Also, there are no entries in the passenger logs.
Is there a setting I should be changing to get this to work?
Apparently, I have to set it up as:
app.get("/uriToApp", (req, res) => {
res.sendFile(__dirname + "/index.html"); });
for app.get() to work.
This post helped:
Phusion Passenger on cPanel (Apache) "Cannot GET" (Express); the same code works on localhost and Heroku

React & Express server not getting api requests in production /build

I have a React app running successfully locally and all api requests are successfully running from a separate server.
When I run a build, the path to the api server is lost and no data is loaded.
Below are a few screenshots...
Loading data successfully from api.
Pointing IIS to react /build folder using localhost:80. No data loading.
Here is an example of an api call in my node/express server/index.js file
app.get('/api/company', (req, res) => {
api_helper.GET('https://****/api/company')
.then(response => {
res.json(response)
})
.catch(error => {
res.send(error)
})
})
My package.json file has the url of the express proxy (running in the background).
"proxy": "http://localhost:5000/",
My question is, why isnt the api loading in production /build? I just get this...
Request URL: http://localhost/api/site
Request Method: GET
Status Code: 404 Not Found
Remote Address: [::1]:80
Referrer Policy: no-referrer-when-downgrade
but when just running locally (npm start) I get this and data loads from api.
Request URL: http://localhost:3000/api/site
Request Method: GET
Status Code: 304 Not Modified
Remote Address: 127.0.0.1:3000
Referrer Policy: no-referrer-when-downgrade
Any help appreciated, driving me mad! Thanks.
After much testing I discovered, you must put the routes before
Wrong Example:
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
app.use('/', routes);
Right Example:
app.use('/api', routes);
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
For anyone else struggling with this, I figured it out..
I had to add this to my express server.js file in the root folder of my project.
app.use(express.static(path.join(__dirname, 'build')));
app.get('/', function (req, res) {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
I then pointed to the address where express is running, in my case http://localhost:5000
This worked.
I also then set up a rewrite rule in IIS to point localhost and our domain name to localhost:5000
All working now, hope it helps someone else.
Thanks for your info. I am quite new to ReactJS and I also encountered similar problems when I created my production build. Actually I had added similar things like
app.use(express.static(<build_folder_dir>));
in my Express Server before then I came to search and see your post. Anyway, I did not add something like the second line of your code and my API calls are written in router created in a separate js file.
app.use('/api/some_path', <imported_router>);
In the exported router object, codes are written like this:
router.get('/some_sub-path')
To make API calls, I used axios in my react app
axios.get(
"/api/some_path"+"/sub-path?param_1="+<param_value>,
{
headers:{
"Content-Type":"application/json",
<some headers>
}
}
).then((res)=>{<Some codes using the res.data.<any param in body>>})
Finally,I added these lines in the server.js
app.get('/*', function(req, res) {
res.sendFile(path.join(__dirname, <path of the index.html in the build dir>), function(err) {
if (err) {
res.status(500).send(err)
}
})
})
Yet, I made a stupid mistake that my app crashed because the app.get overwrite the settings in router. Just a reminder, if you enable any API calls in GET method, use regex to exclude the pattern for making API calls.

How to manually serve files on Parse.com?

I've deployed a single-page app using a frontend framework to my Parse Hosting.
However, there's a huge issue in there: sub-paths get routed to the /public folder, and there's only an index.html and a bunch of assets in there.
I've tried numerous options on serving that static index file through all other routes, by using Express or HTTP in cloud/main.js, but it seems Parse runs a custom subset of Node modules. They've erased all filesystem methods. There's no sendFile() on Express API, no readFile()on fs module...
What can I do to achieve that?? I just need all paths not in the public folder to serve the same thing: my index.html file.
What I've already tried:
Read the file and serve it:
app.use(function(req, res) {
fs.readFile('../public/index.html', 'utf8' , function(err, data) {
if (err) {
console.error(err);
}
res.send(data);
});
});
Serve it as an Express Middleware (probably the most efficient way):
app.use(function(req, res) {
res.sendFile('../public/index.html');
});
Serve it as a catch-all route:
app.get('*', function(req, res) {
res.sendfile('../public/index.html');
});

Express.js App on Phusion Passenger - did not write a startup response in time

I am trying to run an express.js app on a server running Phusion Paggenger (apache) and am seeing the error "An error occurred while starting the web application: it did not write a startup response in time." after the request times out. I've read through https://github.com/phusion/passenger/wiki/Debugging-application-startup-problems but this seems a bit obscure. My express app is as bare-bones as possible so I'm wondering if anyone knows if there may be a component specific to express that might cause this. I have been able to run a plain node.js app with the same setup on the server.
If you used the express-generator command to set up your project, you might see if pointing your Virtual Host configuration file's PassengerStartupFile line to bin/www instead of app.js does the trick instead of explicitly calling app.listen in the app.js file. Phusion Passenger's documentation does not address this specific convention adopted by ExpressJS. You can read some about this bin/www startup convention on Express's Moving to 4.x guide. Seemed to work for me.
It seems that you need to explicitly call app.listen within app.js. Specifically, I do this only when in production:
if (app.get('env') === 'production') {
app.listen(3000);
}
at the end of app.js
If you are getting here from google. This is now documented with Passenger: https://www.phusionpassenger.com/library/indepth/nodejs/reverse_port_binding.html
A working example of a simple express app is below:
if (typeof(PhusionPassenger) != 'undefined') {
PhusionPassenger.configure({ autoInstall: false });
}
var express = require('express');
var app = express();
app.get('/', function(req, res){
var body = 'Hello World';
res.setHeader('Content-Type', 'text/plain');
res.setHeader('Content-Length', body.length);
res.end(body);
});
if (typeof(PhusionPassenger) != 'undefined') {
app.listen('passenger');
} else {
app.listen(3000);
}

SockJS multiplexing example 404 index.html not found

Looking at SockJS multiplex example
I got the server running. When I go to http://127.0.0.1:9999/multiplex I see a welcome message "Welcome to SockJS!"
When I try to browser to index.hmtl http://127.0.0.1:9999/index.html I get 404 message "Cannot GET /index.html"
The 'index.html' is in the same directory as the server.js is running. Why can't the server find this file?
Please double check if you're using good express version. Express 3 changed API's and code may require some tweaking. See sockjs-node/examples for examples.
Installed express 3.1.1 and made some code updates to server.js. Now when I go to http://127.0.0.1:9999/ it serves me index.html as specified in server:
app.get('/', function (req, res) {
res.sendfile(__dirname + '/index.html');
});
Not sure why but http://127.0.0.1:9999/index.html still doesn't give me the file.

Resources