Search code examples
node.jsangulardeploymentcpanelproduction

Production Angular/nodejs app with cPanel. Node.js app error?


I have deployed an angular app via cPanel (compiled with command 'ng build --prod="true"') and its currently live: https://nathankeogh.com

It also uses node.js backend which is running on cPanel. App Startup file is "server.js".

I have imported data from MySQL database with data etc. on cPanel also. (Used command "sed -e 's/utf8mb4_0900_ai_ci/utf8mb4_unicode_ci/g' -i myfilename.sql" to get arround unknown collation error.)

My prod.envrionment.ts file currently points the ApiURL to "httpS://nathankeogh.com".

I have updated the node.js application on cPanel, with the name and credentials for the database on cPanel.

When accessing the website, It loads the login page as expected but as you can see, getting an Internal server error (unknown) in chrome's console and data will not post/get: error

The development version is working as expected.

Further development: When hovering over the "error" text in the chrome console, it shows a full error output:

enter image description here

"Web application could not be started". I believe its a cPanel node application error: enter image description here

Following this, a cPanel error states an issue with server.js file line 7? or 22?. ("app.use('/accounts', require('./accounts/accounts.controller'));") Can anyone identify what is wrong the server.js file causing the issue?

Any help in identifying the problem or advice is very much appreciated. Thank you.

node.js cPanel config: enter image description here

Server.js:

require('rootpath')();
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const cors = require('cors');
const errorHandler = require('_middleware/error-handler');

const multer = require('multer')
//const upload = multer({ dest: 'images/' })
var fileExtension = require('file-extension')

//app.use(express.static(__dirname+'/uploaded-files'));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser());

// allow cors requests from any origin and with credentials
app.use(cors({ origin: (origin, callback) => callback(null, true), credentials: true }));

// api routes
app.use('/accounts', require('./accounts/accounts.controller'));
app.use('/lab-swaps', require('./lab-swaps/lab-swaps.controller'));

// swagger docs route
app.use('/api-docs', require('_helpers/swagger'));

// global error handler
app.use(errorHandler);

// start server
const port = process.env.NODE_ENV === 'production' ? (process.env.PORT || 80) : 4000;
app.listen(port, () => console.log('Server listening on port ' + port));

// Basic Get Route
app.get('/', function (req, res) {
    res.json({ message: 'Server Started!' });
});

var storage = multer.diskStorage({

    // Setting directory on disk to save uploaded files
    destination: function (req, file, cb) {
        cb(null, 'uploaded-files')
    },

    // Setting name of file saved
    filename: function (req, file, cb) {
        cb(null, file.fieldname + '-' + Date.now() + '.' + fileExtension(file.originalname))
    }
})

var upload = multer({
    storage: storage,
    limits: {
        // Setting Image Size Limit to 2MBs
        fileSize: 2000000
    },
    fileFilter(req, file, cb) {
        if (!file.originalname.match(/\.(jpg|jpeg|png)$/)) {
            //Error 
            cb(new Error('Please upload JPG and PNG images only!'))
        }
        //Success 
        cb(undefined, true)
    }
})

app.post('/uploadedImages', upload.single('uploadedImage'), (req, res, next) => {
    const file = req.file
    console.log(req);
    if (!file) {
        const error = new Error('Please upload a file')
        error.httpStatusCode = 400
        return next(error)
    }
    res.status(200).send({
        statusCode: 200,
        status: 'success',
        uploadedFile: file
    })

}, (error, req, res, next) => {
    res.status(400).send({
        error: error.message
    })
})

Solution

  • The rootpath library does not seem well maintained, so it may be causing issues.

    Remove it and use relative paths instead.

    const express = require('express');
    const app = express();
    const bodyParser = require('body-parser');
    const cookieParser = require('cookie-parser');
    const cors = require('cors');
    const errorHandler = require('./_middleware/error-handler');
     
    const multer = require('multer');
    

    Relative paths are more conventional, so you will avoid confusing future developers working on your code, and you will avoid potential issues with your editor.