NodeJS Express JS应用程序显示在cPanel上部署后无法找到,而在本地运行良好

nnt7mjpx  于 2023-01-30  发布在  Node.js
关注(0)|答案(1)|浏览(122)

Express应用程序在cPanel上部署后显示找不到。当我编写server.listen()时,我也试图解决这个问题,它工作得很好,但当我编写app.listen()时,它给出找不到消息。
我尝试了默认的Node Js代码(最后10行,除了app.listen()),当app.listen()不工作时,它工作正常:

const express = require("express");
const multiparty = require('multiparty');
const mongoose = require("mongoose");
const morgan = require('morgan');
const { createHttpTerminator } = require('http-terminator');
const fs = require('fs');
const cors = require('cors');
const crypto = require('crypto');
require('dotenv').config();
const { MongoClient, ServerApiVersion } = require('mongodb');

const {Product, Service, Home, HireMe } = require('./models/Product');

const app = express();

app.use(morgan('tiny'));
app.use(express.static('public'));
app.use(express.json());
app.use(cors());

app.get('/', (req, res) => {
  res.send('Home Page...!');
});

app.get('/offers', async (req, res) => {
    
  try {
    const result = await Product.find({});
    res.send("result");
  } catch (err) {
    res.send({ 'error': err.message });
  }
})


var http = require('http');
var server = http.createServer(function(req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    var message = 'It works!\n',
        version = 'NodeJS ' + process.versions.node + '\n',
        response = [message, version].join('\n');
    res.end(app);
});
server.listen(); //It works
app.listen (); // Showing Cannot find message
iszxjhcz

iszxjhcz1#

我解决这个错误的方法是把URL链接(我在cPanel上创建了 NodeJS )作为路由的前缀,现在它工作得很好。

相关问题