Nextjs13 APP dir 使用体验
在新项目试用一段时间,有几个感受:
router 用起来还是没有 reactrouter(https://reactrouter.com/en/main) 方便,不够自由,但相比 pages 目录
full code:
mocks/http.ts
import { createMiddleware } from '@mswjs/http-middleware';
import express from 'express';
import cors from 'cors';
import { handlers } from './handlers';
const app = express();
const port = 9090;
app.use(cors({ origin: 'http://localhost:3000', optionsSuccessStatus: 200, credentials: true }));
app.use(express.json());
app.use(createMiddleware(...handlers));
app.listen(port, () => console.log(`Mock server is running on port: ${port}`));
package.json
{
"scripts": {
"dev": "next dev",
"mock": "npx tsx ./mocks/http.ts",
}
}
axios settings:
axios.defaults.baseURL = 'http://localhost:9090';
start mock server yarn mock
, start dev server yarn dev