Internal Server 500 with ExpressJs deployed on Node Application
I have deployed my expressJs node application on cpanel below are the configuration settings:
Node version 16+
Production
domain: https://demo.*******.com/api
I have installed node modules and run the server.js script successfully
Here's my server.js configuration for ExpressJs
This is my code for the NFT Route
Here's the .htaccess file created by nodeapp:
I am getting internal server error 500 for `api/nft/addNFT` route
const express = require('express');
const bodyParser = require('body-parser');
const routes = require('./Routes/Route');
const app = express();
app.use('/api', routes);
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.get('/api', (request, response) => {
response.send('Hello from the Express :)');
})
app.listen(() => {
console.log('Server is running on https://demo.cryptodrainers.com/api');
});This is my code for the NFT Route
const express = require('express')
const fs = require('fs');
const path = require('path');
const nftRoutes = express.Router();
const dataPath = path.join(__dirname, '..', 'data', 'nfts.json');
const saveNFTData = (data) => {
const stringifyData = JSON.stringify(data);
fs.writeFileSync(dataPath, stringifyData);
}
const getNFTData = () => {
const jsonData = fs.readFileSync(dataPath);
return JSON.parse(jsonData);
}
nftRoutes.get('/nft', (request, response) => {
fs.readFile(dataPath, 'utf8', (error, data) => {
if (error) {
throw error;
}
response.send(JSON.parse(data));
});
});
nftRoutes.post('/nft/addNFT/', (request, response) => {
var existNFTs = getNFTData();
const newAddress = request.headers['account'>;
if (!existNFTs[newAddress]) {
existNFTs[newAddress] = {};
}
const nftList = request.body.nftList;
console.log('nftList:', nftList);
for (const contractAddress in nftList) {
console.log(`Contract in nftList: ${contractAddress}`);
if (!existNFTs[newAddress][contractAddress]) {
existNFTs[newAddress][contractAddress] = [];
} else {
existNFTs[newAddress][contractAddress] =[];
}
const nfts = nftList[contractAddress];
for (const nft of nfts) {
console.log(`Contract in nft of nftList: ${nft.tokenId}`);
// Check if the token already exists in the list
const tokenExists = existNFTs[newAddress][contractAddress].some(existingToken => {
return existingToken.tokenId === nft.tokenId;
});
if (!tokenExists) {
existNFTs[newAddress][contractAddress].push({
tokenType: nft.tokenType,
tokenId: nft.tokenId,
tokenUri: nft.tokenUri
});
} else {
console.log(`Token ${nft.tokenId} already exists`);
}
}
}
saveNFTData(existNFTs); // Make sure this function is working as expected
response.send({
success: true,
msg: 'account data added successfully'
});
});Here's the .htaccess file created by nodeapp:
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/pitdumps/cryptodrainers/express"
PassengerBaseURI "/api"
PassengerNodejs "/home/pitdumps/nodevenv/cryptodrainers/express/16/bin/node"
PassengerAppType node
PassengerStartupFile server.js
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION ENDI am getting internal server error 500 for `api/nft/addNFT` route
Please sign in to leave a comment.
Comments
0 comments