在cpanel上运行nodejs网站 [英] Running a nodejs website on cpanel
问题描述
您好,
我已将NodeJS安装到我的cPanel帐户上,然后我将我的NodeJS网站/应用程序上传到我的public_html /网站,然后我添加了以下代码我的htaccess:
RewriteEngine On
RewriteRule ^ $ http: // localhost:51521 / [P,L]
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}! - d
RewriteRule ^ $ http: // localhost:51521 / $ 1 [P,L]
将代码添加到.htaccess后,我运行了NodeJS应用程序,网站运行了,主页看起来很好,所有那里,但是当我转到任何其他页面时,例如转到关于我的页面,它显示未找到在此服务器上找不到请求的URL / about。
[404未找到图片] [1]
然后我继续制作.htaccess稍微改变:** ^ $ **到最后一行的** ^(。*)$ **
< pre lang =Javascript> RewriteEngine On
RewriteRule ^ $ http: // localhost:51521 / [P ,L]
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME}!-d
RewriteRule ^(。*)$ http: // localhost:51521 / $ 1 [P,L]
这导致网站通过错误显示:404:index.php并加载了主页,但遗漏了很多东西,但是!其他页面显示但不完全在那里..这里是主页的图像:[主页调整后] [2],这里是第二页的图片显示在调整前的404:[第二页] [3]
我一直在研究不停,我仍然找不到解决方案,但这里是我的nodejs执行代码:
var createError = require(' HTTP的错误跨度>);
var express = require(' express' 跨度>);
var path = require(' path' 跨度>);
var cookieParser = require(' cookie-解析器'跨度>);
var logger = require(' morgan' 跨度>);
var mongoose = require(' mongoose' 跨度>);
var methodOverride = require(' method-倍率'跨度>);
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(' ./路由/索引'跨度>);
var usersRouter = require(' ./路由/用户的跨度>);
var app = express();
// 查看引擎设置
app.set(< span class =code-string>' 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,' 公共'跨度>)));
app.use(' / uploads',express。 static (path.join(__ dirname,' uploads')));
app.use(' /',indexRouter);
app.use(' / users',usersRouter);
// 捕获404并转发到错误处理程序
app .use( function (req,res,next){
next(createError( 404 ) );
});
// 错误处理程序
app.use( function (错误,req,res,next){
// 设置本地,仅在开发中提供错误
res.locals.message = err.message;
res.locals.error = req.app.get(' env')=== ' 开发?错误:{};
// 渲染错误页面
res.status(err.status || 500 );
res.render(' 错误');
});
app.listen(process.env.PORT || 51521 ,()=> {
console .log(' 服务器位于');
});
这是nodejs网站文件的图片:[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
我真的很感谢你们的帮助:)
非常喜欢
我尝试了什么:
好像我已经尝试了一切..:/
http: // localhost:51521 / [P, L]
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME} !-d
RewriteRule ^
http: // localhost: 51521 /
1 [P,L]
添加该代码后到.htaccess,我运行了NodeJS应用程序,网站运行了,主页看起来很好,但是当我去任何其他页面时,例如转到关于我页面,它显示未找到请求在这台服务器上找不到URL / about。
[404找不到图片] [1]
然后我继续做了一点更改.htaccess:** ^
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:
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
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:
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.. :/
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:"**^
这篇关于在cpanel上运行nodejs网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!