首页 > Web开发 > 详细

react: nextJs koa project basic structure

时间:2019-06-15 21:26:54      阅读:84      评论:0      收藏:0      [点我收藏+]

1、init nextJs project

npm init

npm install react react-dom next

config script in package.json

"dev": "next"

"start": "next start" 

"build": "next build"

npm run dev

result: 404 page not found

2、index.js entry file

export default () => <span>hello react next<span>

result:  hello react next

3、koa server

npm install  koa koa-router

const Koa = require(‘koa‘);
const next = require(‘next‘);

const dev = process.env.NODE_ENV !== "production";
//创建next app处于开发状态
const app = next({ dev });

const handle = app.getRequestHandler();

//页面加载编译完成后在处理请求
app.prepare().then(() => {
    const server = new Koa();
    //中间件的使用
    server.use(async (context, next) => {
        //request,response,req,res;await next() 执行下一个中间件
        await handle(context.req, context.res);
        context.respond = false;
    });
    server.listen(3000, () => {
        console.log("koa server listening on 3000")
    })
})

update script

"dev": "node server.js"

4、next with antd and css

npm install antd @zeit/next-css babel-plugin-import

for css config next.config.js

const withCss = require(‘@zeit/next-css‘);

if (typeof require !== ‘undefined‘) {
    require.extensions[‘.css‘] = file => {}
}

module.exports = withCss({})

for antd config .babelrc

{
    "presets": ["next/babel"],
    "plugins": [
        [
            "import",
            {
                "libraryName": "antd",
                "style": "css"
            }
        ]
    ]
}

test valid 

app.js

import App from "next/app";

import "antd/dist/antd.css";

export default App

update index.js

import { Button } from "antd";
export default () => <Button type="primary">hello world</Button>

 

react: nextJs koa project basic structure

原文:https://www.cnblogs.com/Nyan-Workflow-FC/p/11028788.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!