我似乎无法弄清楚我的问题......
我的节点应用程序通过 VS Codes 终端和 Heroku 客户端部署到 Heroku 工作正常。 它无法通过 nodejs 选择器“Setup NodeJS App”部署到 A2hosting。它显示没有样式表的页面,然后刷新并得到下面的 403 错误,再次刷新获取没有样式表的页面,刷新回 403 并像这样一直循环 - 很奇怪。
403 错误: 未找到 404
NotFoundError: Not Found
at /home/wyoungah/wet_node/app.js:56:8
at Layer.handle [as handle_request] (/home/wyoungah/nodevenv/wet_node/14/lib/node_modules/express/lib/router/layer.js:95:5)
at trim_prefix (/home/wyoungah/nodevenv/wet_node/14/lib/node_modules/express/lib/router/index.js:317:13)
at /home/wyoungah/nodevenv/wet_node/14/lib/node_modules/express/lib/router/index.js:284:7
at Function.process_params (/home/wyoungah/nodevenv/wet_node/14/lib/node_modules/express/lib/router/index.js:335:12)
at Immediate.next (/home/wyoungah/nodevenv/wet_node/14/lib/node_modules/express/lib/router/index.js:275:10)
at Immediate._onImmediate (/home/wyoungah/nodevenv/wet_node/14/lib/node_modules/express/lib/router/index.js:635:15)
at processImmediate (internal/timers.js:463:21)
package.json
{
"name": "avwet",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"adds": "^1.2.9",
"cookie-parser": "~1.4.4",
"debug": "~2.6.9",
"ejs": "~2.6.1",
"express": "~4.16.1",
"helmet": "^4.4.1",
"http-errors": "~1.6.3",
"moment": "^2.29.1",
"morgan": "~1.9.1"
}
}
app.js
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
const ADDS = require('adds');
const helmet = require('helmet');
var indexRouter = require('./routes/index');
// var usersRouter = require('./routes/users');
var wetRouter = require('./routes/wet_av');
var app = express();
//touch tmp/restart.txt
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
let setCache = function (req, res, next) {
// here you can define period in second, this one is 5 minutes
const period = 60 * 5
// you only want to cache for GET requests
if (req.method == 'GET') {
res.set('Cache-control', `public, max-age=${period}`)
} else {
// for the other requests set strict no caching parameters
res.set('Cache-control', `no-store`)
}
// remember to call next() to pass on the request
next()
}
// now call the new middleware function in your app
app.use(setCache);
app.use(helmet());
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('/', indexRouter);
// app.use('/users', usersRouter);
app.use('/wet_av', wetRouter);
// 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');
});
module.exports = app;
bin www 文件
#!/usr/bin/env node
/**
* Module dependencies.
*/
var app = require('../app');
var debug = require('debug')('avwet:server');
var http = require('http');
/**
* Get port from environment and store in Express.
*/
var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
/**
* Create HTTP server.
*/
var server = http.createServer(app);
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);
if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}
/**
* Event listener for HTTP server "error" event.
*/
function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}
var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;
// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}
/**
* Event listener for HTTP server "listening" event.
*/
function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
head.ejs
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BmbxuPwQa2lc/FVzBcNJ7UAyJxM6wuqIj61tLrc4wSX0szH/Ev+nYRRuWlolflfl" crossorigin="anonymous">
<link rel="stylesheet" href="/stylesheets/style.css">
<title>Aviation Weather App</title>
</head>
文件结构 file structure from "File Manager"
感谢您的帮助........