Wednesday, 20 November 2019

Node JS deployment

I tried to put my Node JS app for my student project.

Here is the project :

Project

N.B: Public is the result of an "npm run build from the front made with Vue JS"

When i work locally, I use a specif path to call request from my back. Example : "http://localhost:3000/read/chosenemployee/"

Code example

But when I put my project online, I use the command "npm start" and use a different port (for example 3535) and replace the "http://localhost:3000" by "https://localhost:3535" and I have this error "ERR_CONNECTION_REFUSED". I also tried :

But nothing change.

Here is my 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)
});

And here is my .htaccess put at the root of my public_html folder :

#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>

And the online folder :

Online folder



from Node JS deployment

No comments:

Post a Comment