简体   繁体   中英

Namecheap: Node JS Express App - App Route return 404 not found

Trying to get Simple Express Application up using NameCheap Shared Hosting.

I have set up my Node JS application as Described here NodeJS NameCheap Docs

Current Setup:

Application Root: url.com
Application URL: url.com
Application Startup File: server.js

I have ran NPM Install using the button provided

I have tried loading the URL http://url.com/hello Expecting Hello World to displayed in the Page.

var express = require("express");
var app = express();
const port = 3001;

app.set("port", port);

app.get("/hello", function(req, res) {
  res.send("hello world");
});

app.listen(app.get("port"), () =>
  console.log("Started listening on %s", app.get("port"))
);

The results I am getting when navigating to http://url.com/hello :

Not Found
The requested URL /index.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.

Namecheap only tells you how to configure the nodejs app however their hosting is based on cPanel which requires you configure the webserver (apache generally). Once you get an application running there's a special button to register it for the apache configuration aka let it run from your domain. I don't know the steps by heart but you should ask NC support to direct you to their documentation for configuring apache to run a nodejs app you configured. If they do not link an article from their knowledge base use this link: https://confluence1.cpanel.net/plugins/servlet/mobile?contentId=17190639#content/view/17190639 Basically what you need now is to configure cPanel or ssh into your server and test your app locally. There's a number of things that could cause your issues like incorrect apache configuration (your default port 80 is looking for php app), port not open/firewalled, application not registered - and all of this is cPanel specific. To make sure you are reading the correct document check in namecheap cpanel for the docs button and review all the above. It should be obvious what needs configured - your nodejs code is probably not the cause here

In my case, it was the problem with .htaccess file. Adding the following rules in my .htaccess file present in the website's public directory helped me:

# CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/<user>/<your_nodejs_app_folder>"
PassengerBaseURI "/."
PassengerNodejs "/home/<user>/nodevenv/<nodejs_app>/<version>/bin/node"
PassengerAppType node
PassengerStartupFile <startup_script>.js
# CLOUDLINUX PASSENGER CONFIGURATION END

Make the required changes in the above rules before pasting them in your .htaccess file. Also, just in case, make sure the port you are using is open, via customer support.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM