我试图把我的Node JS应用程序放到我的学生项目中。
这是项目:
N.B:Public是使用Vue JS创建的前端经过“npm run build”后的结果
当我在本地工作时,我使用特定的路径来调用后端请求。例如:“http://localhost:3000/read/chosenemployee/”
但是当我将我的项目放到网上时,我使用命令“npm start”并使用不同的端口(例如3535),并将“http://localhost:3000”替换为“https://localhost:3535”,然后出现错误“ERR_CONNECTION_REFUSED”。我还尝试了以下方法:
但是没有任何改变。
这是我的app.js:
//1- CREATION DES DEPENDANCES DE MODULES
//MODULE DE JS.NODE
// const https = require('https');
// const fs = require('fs');
// var http = require("http");
const express = require('express');
const app = express();
//Paramétrage du CORS afin qu'il n'y ai pas de blocage
app.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
const router = express.Router();
const cors = require("cors");
const mysql = require('mysql');
const bodyParser = require('body-parser');
const mysqlApostrophe = require("mysql-apostrophe");
//Module permettant de faire des refresh en SPA
const history = require('connect-history-api-fallback');
//IMPORT DES MODULES CREES
var dataBase = require('./routes/dataBase');
//ATTENTION SEMBLE POSER PROBLEME A NODE EN LOCAL
app.use(history());
app.use(cors());
//2- MISE EN PLACE DU BODY PARSER QUI PERMET DE LIRE LES JSON ET URL ENVOYE PAR LE FORMULAIRE
app.use(bodyParser.json()); // LIRE LES BODY ENCODES EN JSON
app.use(bodyParser.urlencoded({ // LIRE LES BODY ENCODES EN URL
extended: true
}));
//Mise en place de express
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({
extended: false
}))
//3- MISE EN PLACE DE mysqlApostrophe
app.use(mysqlApostrophe); //PERMET D'INSERER DES CHAMPS CONTENANT DES APOSTROPHES
//4- RECUPERATION DES FICHIERS ROUTES DANS LE DOSSIER ROUTES
const creation = require("./routes/create");
const lecture = require("./routes/read");
const maj = require("./routes/update");
const suppression = require("./routes/delete")
//5- UTILISATION DES ROUTES
app.use("/create", creation);
app.use("/read", lecture);
app.use("/update", maj);
app.use("/delete", suppression)
//Gestion de la mise en production
if (process.env.NODE_ENV === 'production') {
//Static folder
app.use(express.static(__dirname + '/public/')).use
//Handle SPA
app.get(/.*/, (req, res) => res.sendFile(__dirname + '/public/index.html'));
} else {
//Static folder
app.use(express.static(__dirname + '/public/')).use
//Handle SPA
app.get('/', (req, res) => res.sendFile(__dirname + '/public/index.html'));
}
//4- CHOIX DU PORT UTILISE PAR LE SERVEUR
const port = process.env.PORT || 8898; //RECUPERE UN PORT LIBRE SINON 3000
app.listen(port, function () {
console.log("Le serveur utilise le port : " + port)
});
这是我放在public_html文件夹根目录下的.htaccess:
#1. Forcing HTTPS connection:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#2. Removing port from URL:
Options +FollowSymLinks -Indexes
IndexIgnore *
DirectoryIndex
# Add headers to all responses.
<IfModule mod_headers.c>
RequestHeader set X-Forwarded-Proto https
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
# Simple URL redirect:
RewriteRule ^(.*)$ http://127.0.0.1:8898/$1 [P]
</IfModule>
在线文件夹:
RewriteRule ^(.*)$ http://127.0.0.1:8898/$1 [P]
不是会将所有请求都重写到 localhost:8898 吗?启用它后就没有办法远程连接了。 - Klaycon