NodeJS 404-页面未找到页面未找到

huwehgph  于 12个月前  发布在  Node.js
关注(0)|答案(1)|浏览(117)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受回答。

这个问题是由错字或无法再重现的问题引起的。虽然类似的问题在这里可能是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
4天前关闭。
Improve this question
这是我的server.js

const express = require('express');
const multer = require('multer');
const axios = require('axios');
const path = require('path');
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const compression = require('compression');
const morgan = require('morgan');
const { body, validationResult } = require('express-validator');
require('dotenv').config({ path: 'a.env' });
 // Load environment variables from a .env file

if (!process.env.SKIN_API_KEY || !process.env.EYE_API_KEY) {
  console.error('Required environment variables are not set.');
  process.exit(1);
}

const app = express();
const port = process.env.PORT || 3000;

// Use security headers with the 'helmet' middleware
app.use(helmet());

// Apply rate limiting to prevent abuse of the endpoints
const limiter = rateLimit({
  windowMs: 15 * 60 * 1000, // 15 minutes
  max: 100, // Limit each IP to 100 requests per windowMs
});
app.use('/skin/upload', limiter);
app.use('/eye/upload', limiter);

// Enable gzip compression for responses
app.use(compression());

// Enable structured request logging with 'morgan'
const morganFormat = ':method :url :status :response-time ms - :res[content-length]';
app.use(morgan(morganFormat));

app.use(express.static(path.join(__dirname, 'public')));
app.use(express.json());

// Configure Multer for file upload
const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

app.get('/skin_model.html', (req, res) => {
  res.sendFile(__dirname + '/public/skin_model.html');
});

app.get('/eye_model.html', (req, res) => {
  res.sendFile(__dirname + '/public/eye_model.html');
});

app.post('/skin/upload', [
  upload.single('image'),
], async (req, res) => {
  console.log('Received Skin Disease Detection request.');
  console.log('Request Body:', req.body);
  console.log('Request File:', req.file);

  // Check if req.file exists and is not undefined
  if (!req.file) {
    console.error('Image file is required.');
    return res.status(400).json({ error: 'Image file is required' });
  }
  
  const data = req.file.buffer;

  try {
    console.log('Sending request to Hugging Face API for Skin Disease Detection.');
    const response = await axios.post(
      'https://api-inference.huggingface.co/models/SM200203102097/skinDiseasesDetectionModel',
      data,
      {
        headers: {
          Authorization: `Bearer ${process.env.SKIN_API_KEY}`,
          'Content-Type': 'image/jpeg',
        },
      }
    );

    if (response.status === 200) {
      console.log('Hugging Face API response:', response.data);
      res.json(response.data);
    } else {
      console.error('Hugging Face API Error:', response.status, response.statusText);
      res.status(response.status).send('Error processing the image.');
    }
  } catch (error) {
    console.error('Server Error:', error);
    res.status(500).send('Error processing the image.');
  }
});

app.post('/eye/upload', [
  upload.single('image'),
], async (req, res) => {
  console.log('Received Skin Disease Detection request.');
  console.log('Request Body:', req.body);
  console.log('Request File:', req.file);

  // Check if req.file exists and is not undefined
  if (!req.file) {
    console.error('Image file is required.');
    return res.status(400).json({ error: 'Image file is required' });
  }

  const data = req.file.buffer;

  try {
    console.log('Sending request to Hugging Face API for Eye Disease Detection.');
    const response = await axios.post(
      'https://api-inference.huggingface.co/models/SM200203102097/eyeDiseasesDetectionModel',
      data,
      {
        headers: {
          Authorization: `Bearer ${process.env.EYE_API_KEY}`,
          'Content-Type': 'image/jpeg',
        },
      }
    );

    if (response.status === 200) {
      console.log('Hugging Face API response:', response.data);
      res.json(response.data);
    } else {
      console.error('Hugging Face API Error:', response.status, response.statusText);
      res.status(response.status).send('Error processing the image.');
    }
  } catch (error) {
    console.error('Server Error:', error);
    res.status(500).send('Error processing the image.');
  }
});

// Custom error handling middleware
app.use((err, req, res, next) => {
  console.error('Unhandled Error:', err);
  res.status(500).send('Internal Server Error');
});

app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

字符串
这是我在循环网站上错误日志

Unhandled Error: [Error: ENOENT: no such file or directory, stat '/var/task/public/skin_model.html'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'stat',
  path: '/var/task/public/skin_model.html',
  expose: false,
  statusCode: 404,
  status: 404
}
 GET /skin_model.html 500 31.726 ms - 21
500 03:01:31.934 (0.034s)


我的网站在localhost完美工作,但不是在循环无服务器主机网站我已经创建了这个网站的git库,并比创建与循环网站和我的网站index.html intermediate_eye.html intermediate_skin.html启动正常,但skin_model.html和eye_model.html显示的帮助主机网站

Unhandled Error: [Error: ENOENT: no such file or directory, stat '/var/task/public/skin_model.html'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'stat',
  path: '/var/task/public/skin_model.html',
  expose: false,
  statusCode: 404,
  status: 404
}
 GET /skin_model.html 500 18.524 ms - 21
 Unhandled Error: [Error: ENOENT: no such file or directory, stat '/var/task/public/skin_model.html'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'stat',
  path: '/var/task/public/skin_model.html',
  expose: false,
  statusCode: 404,
  status: 404
}
 GET /skin_model.html 500 1.662 ms - 21
 GET /favicon.ico 404 1.790 ms - 150
 Unhandled Error: [Error: ENOENT: no such file or directory, stat '/var/task/public/eye_model.html'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'stat',
  path: '/var/task/public/skin_model.html',
  expose: false,
  statusCode: 404,
  status: 404
}


这是我电话簿

Diseases Detection Website1
├── public/
│   ├── image/
│   ├── index.html
│   ├── intermediate_eye.html
│   ├── intermediate_skin.html
│   ├── main_script.js
│   ├── main_style.css
│   ├── skin_model.html
│   ├── skin_model.js
│   ├── eye_model.html
│   ├── eye_model.js
│   ├── model_page_style.css
│
├── server.js
├── package.json

mgdq6dx1

mgdq6dx11#

始终仔细检查您在所有HTML,CSS和JS文件中分配的文件名。虽然它们可能在localhost中完美工作,但当您在服务器上托管文件时,命名中的小错误可能会导致重大错误。在我的情况下,我忽略了一个看似微不足道的错误,导致我的代码在localhost中完美运行,但在托管时失败。错误。因此,总是要勤奋检查。另一个重要的一点:如果你的CSS在主机上不起作用,确保你已经添加了所有正确的Meta标签。这成为我的项目中的另一个挑战。

相关问题