admin管理员组文章数量:1123798
When running the netlify dev
command, the endpoints (home
and create
) in user/routes.js
are not found (404). So I cannot deploy on production.
|---.git
|---.gitignore
|---src
| |---user
| | |---controller.js (ignore this part)
| | |---routes.js
| |---app.js
| |---config.js
| |---routes.js
|---.env.dev.sample
|---package.json
// src/user/routes.js
import express from 'express';
import { create } from './controller.js';
const router = express.Router();
router.get('/create', create);
router.get('/test', (req, res) => {
res.send("Test page for netlify hosting");
});
export default router;
// src/app.js
import express from 'express';
import cors from 'cors';
import { APP_PORT } from './config.js';
import router from './routes.js';
import serverless from 'serverless-http';
const app = express();
app.listen(APP_PORT, () => {
console.log(`Server is running on port ${APP_PORT}`);
});
app.use(express.json({ limit: '5mb' }));
app.use(cors());
app.use('/api/v1', router);
app.use((req, res, next) => {
res.status(404).json('The requested resource was not found on this server');
});
export default serverless(app);
// src/config.js
import dotenv from 'dotenv';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import path from 'path';
const envConfigFile = process.env;
if (envConfigFile.NODE_ENV !== 'prod') {
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const configFile = path.resolve(__dirname, '../.env.sample');
dotenv.config({ path: configFile, encoding: 'utf-8' });
}
const { APP_PORT } = envConfigFile;
export { APP_PORT };
// src/routers.js
import { Router } from 'express';
import userRoutes from './user/routes.js';
const router = Router();
router.use('/user', userRoutes);
export default router;
# .env.dev.sample
APP_PORT = "yourAppPort"
// package.json
{
"version": "1.0.0",
"description": "",
"type": "module",
"engines": {
"node": "20.18.0",
"npm": "10.8.2"
},
"main": "src/app.js",
"scripts": {
"start": "cross-env NODE_ENV=prod node src/app.js",
"devStart": "cross-env NODE_ENV=dev nodemon src/app.js"
},
"author": "",
"license": "",
"homepage": "",
"devDependencies": {
"cross-env": "7.0.3",
"nodemon": "3.1.9"
},
"dependencies": {
"cors": "2.8.5",
"dotenv": "16.4.7",
"express": "4.21.2",
"serverless-http": "^3.2.0"
}
}
Already try this. Still I cannot deploy on local and prod on Netlify site.
本文标签: How to deploy Nodejs on NetlifyStack Overflow
版权声明:本文标题:How to deploy Node.js on Netlify? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736594604a1945129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论