執行 npm run dev 後出現 404 (Not Found) 的訊息

来源:2-3 next作为koa中间件使用

elftvxq

2021-01-03

老師您好,跟著影片打代碼執行 npm run dev 出現404的訊息,以下是我的 server.js 檔案,不太確定是什麼問題造成的,想請老師看看

錯誤訊息
图片描述

server.js

const Koa = require('koa')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
	const server = new Koa()

	server.use(async (ctx, next) => {
		await handle(ctx.req, ctx.res)
		ctx.respond = false
	})

	server.listen(3000, () => {
		console.log('koa server listening on 3000')
	})
})

package.json 檔

{
	"name": "nextjs-project",
	"version": "1.0.0",
	"description": "",
	"main": "index.js",
	"scripts": {
		"test": "echo \"Error: no test specified\" && exit 1",
		"dev": "node server.js",
		"buid": "next build",
		"start": "next start"
	},
	"repository": {
		"type": "git",
		"url": "git+https://github.com/elftvxq/nextjs-project.git"
	},
	"author": "",
	"license": "ISC",
	"bugs": {
		"url": "https://github.com/elftvxq/nextjs-project/issues"
	},
	"homepage": "https://github.com/elftvxq/nextjs-project#readme",
	"dependencies": {
		"koa": "^2.13.0",
		"next": "^10.0.4",
		"react": "^17.0.1",
		"react-dom": "^17.0.1"
	}
}

写回答

1回答

Jokcy

2021-01-04

建议把nextjs退到8.x版本试一下

1
1
elftvxq
可以執行了~ 感謝老師
2021-01-04
共1条回复

全栈进阶课程 React16.8+Next.js+Koa2一步到位开发Github

学习React/Next.js服务端渲染SSR同构设计方案,理解OAuth登录体系的实现原理

651 学习 · 311 问题

查看课程