A
A
ArtGMlg2020-10-10 13:48:27
Node.js
ArtGMlg, 2020-10-10 13:48:27

Node.js express hosted application sending a 404 status response?

There is some apache host, one project with a php server already exists on it. I'm trying to add another project there, but already based on the node.js express server.
Here is the app.js file itself

var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var http = require('http');

var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users.js');
var loginRouter = require('./routes/login.js');
var tasksRouter = require('./routes/tasks.js');
var groupsRouter = require('./routes/groups.js');
var bodyParser = require('body-parser');
var imageRouter = require('./routes/image.js');
var weatherRouter = require('./routes/weather.js');
var citiesRouter = require('./routes/cities.js');
var sendEmailRouter = require('./routes/email.js');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use(bodyParser.urlencoded({ extended: true }));

app.use('/', indexRouter);
app.use('/users', usersRouter);
app.use('/login', loginRouter);
app.use('/tasks', tasksRouter);
app.use('/groups', groupsRouter);
app.use('/image', imageRouter);
app.use('/weather', weatherRouter);
app.use('/cities', citiesRouter);
app.use('/email', sendEmailRouter);

// 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');
});

http.createServer(app).listen(8080, function() {
  console.log('Listening to port:  ' + 8080);
});

module.exports = app;


And one of the routers (indexRouter)

var express = require('express');
var router = express.Router();

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { 
  title: 'Ежедневник',
  tasks: ["1", "2","3","4","asd",]
  });
});

module.exports = router;


If I specify the application URL www.example.com in the .htaccess file, everything works as it should. but if I want to access the application not directly, but at www.example.com/my-server, then there are problems.

.htaccess file:
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/hostXXXXXXX/domains/example.com/public_html/my-server"
PassengerBaseURI "/my-server"
PassengerNodejs "/home/hostXXXXXXX/nodevenv/domains/example.com/public_html/my-server/10/bin/node"
PassengerAppType node
PassengerStartupFile app.js
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END


If app.use('/', indexRouter);indexRouter is replaced by
function(req, res){
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('Hello World\n');
}

then the line Hello World comes,
otherwise the line Cannot GET /my-server/ comes, and the response status itself becomes 404

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
ArtGMlg, 2020-10-11
@ArtGMlg

As a result, the solution to the problem turned out to be much simpler than expected. To be able to access the application and its routers, respectively, located in the hosting subdirectory, it was necessary to use instead . Surprisingly, there is very little material on the Internet on this issue. Here is a link to an article with a solution: https://medium.com/better-programming/serving-a-no...app.use('/', indexRouter);app.use('/my-server', indexRouter);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question