Click here to Skip to main content
16,017,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have installed NodeJS onto my cPanel account, then I uploaded my NodeJS website/app onto my public_html/website then I added the following code to my htaccess:

JavaScript
RewriteEngine On
    RewriteRule ^$ http://localhost:51521/ [P,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^$ http://localhost:51521/$1 [P,L]


After adding that code to .htaccess, I ran the NodeJS application, and the website ran, and the homepage looked fine and all there, but when I go to any other pages, like for an example go to the About me page, It shows Not Found The requested URL /about was not found on this server.
[404 not found picture][1]

Then I proceeded to make a slight change in the .htaccess:"**^$**" to "**^(.*)$**" in the last line

JavaScript
RewriteEngine On
    RewriteRule ^$ http://localhost:51521/ [P,L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ http://localhost:51521/$1 [P,L]


This caused the website to through an error showing: "404: index.php" and the homepage loaded but was missing a lot of stuff, BUT! the other pages showed but not fully there.. here is an image of the homepage: [homepage after tweak][2] and here's a picture of the second page that was showing 404 before the tweaking: [second page][3]

I have been researching non stop and I still can't find the solution, but here is my nodejs execution code:

JavaScript
var createError = require('http-errors');
    var express = require('express');
    var path = require('path');
    var cookieParser = require('cookie-parser');
    var logger = require('morgan');
    var mongoose =require('mongoose');
    var methodOverride = require('method-override');
    var nodemailer = require('nodemailer');
    
    // mongoose.connect('mongodb://jehad:jhadayman1@ds121268.mlab.com:21268/hoca');
    mongoose.connect('mongodb://jhad96:jhad96@ds121268.mlab.com:21268/hoca');
    mongoose.Promise = global.Promise;
    
    var indexRouter = require('./routes/index');
    var usersRouter = require('./routes/users');
    
    var app = express();
    
    // view engine setup
    app.set('views', path.join(__dirname, 'views'));
    app.set('view engine', 'ejs');
    
    app.use(logger('dev'));
    app.use(express.json());
    app.use(express.urlencoded({ extended: false }));
    app.use(cookieParser());
    app.use(methodOverride("_method"));
    app.use(express.static(path.join(__dirname, 'public')));
    app.use('/uploads',express.static(path.join(__dirname, 'uploads')));
    
    app.use('/', indexRouter);
    app.use('/users', usersRouter);
    
    // catch 404 and forward to error handler
    app.use(function(req, res, next) {
      next(createError(404));
    });
    
    // error handler
    app.use(function(err, req, res, next) {
      // set locals, only providing error in development
      res.locals.message = err.message;
      res.locals.error = req.app.get('env') === 'development' ? err : {};
    
      // render the error page
      res.status(err.status || 500);
      res.render('error');
    });
    
    app.listen(process.env.PORT || 51521, ()=>{
      console.log('server is on ');
    });

Here is a picture of the nodejs website files: [files][4]


[1]: https://i.stack.imgur.com/hwsyS.png
[2]: https://i.stack.imgur.com/sqds1.png
[3]: https://i.stack.imgur.com/dsZOi.png
[4]: https://i.stack.imgur.com/gjOAr.png

I really appreciate your help guys :)
much love

What I have tried:

seems that I have tried everything.. :/
Posted
Updated 25-Oct-19 14:56pm

1 solution

Solution is a work in progress.

I've been working to try and fix this same problem for many hours now.

What I've learned so far ...

1. The issue lies with express.static not working as it should. It seems to not be able to find the file in public directory

2. The issue only exists running under cPanel. If I run node manually and connect directly to it (port 3000 in my case) then everything works as it should.

3. My cPanel (later version than OPs?) supports nodejs via Passenger, "natively". However, it makes no difference if I use the cPanel built-in method or the same style as OP.

4. None of this makes any sense ... especially no.3.

5. Seems almost no one else anywhere is having this problem. Even the most simple, stripped down examples and how-tos, which evidently work just fine for everyone else, do NOT work for the OP and myself. WTF? :-/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900