• react 服务端渲染(ssr) 框架 Next.js


    官方网站:https://nextjs.org/

    deom:https://github.com/xutongbao/hello-next-fetching-data

    index.js:

    import Layout from '../components/MyLayout.js'
    import Link from 'next/link'
    import fetch from 'isomorphic-unfetch'
    
    const Index = (props) => (
      <Layout>
        <h1>Batman TV Shows</h1>
        <ul>
          {props.shows.map(({show}) => (
            <li key={show.id}>
              <Link as={`/p/${show.id}`} href={`/post?id=${show.id}`}>
                <a>{show.name}</a>
              </Link>
            </li>
          ))}
        </ul>
      </Layout>
    )
    
    Index.getInitialProps = async function() {
      const res = await fetch('https://api.tvmaze.com/search/shows?q=batman')
      const data = await res.json()
    
      console.log(`Show data fetched. Count: ${data.length}`)
    
      return {
        shows: data
      }
    }
    
    export default Index

    about.js:

    import Layout from '../components/MyLayout.js'
    
    export default () => (
        <Layout>
           <p>This is the about page</p>
        </Layout>
    )

    post.js:

    import Layout from '../components/MyLayout.js'
    import fetch from 'isomorphic-unfetch'
    
    const Post =  (props) => (
        <Layout>
           <h1>{props.show.name}</h1>
           <p>{props.show.summary.replace(/<[/]?p>/g, '')}</p>
           <img src={props.show.image.medium}/>
        </Layout>
    )
    
    Post.getInitialProps = async function (context) {
      const { id } = context.query
      const res = await fetch(`https://api.tvmaze.com/shows/${id}`)
      const show = await res.json()
    
      console.log(`Fetched show: ${show.name}`)
    
      return { show }
    }
    
    export default Post

    Header.js:

    import Link from 'next/link'
    const linkStyle = {
      marginRight: 15
    }
    
    const Header = () => (
      <div>
        <Link href="/">
          <a style={linkStyle}>Home</a>
        </Link>
        <Link href="/about">
          <a style={linkStyle}>About</a>
        </Link>
      </div>
    )
    
    export default Header

    MyLayout.js:

    import Header from './Header'
    
    const layoutStyle = {
      margin: 20,
      padding: 20,
      border: '1px solid #DDD'
    }
    
    const Layout = (props) => (
      <div style={layoutStyle}>
        <Header />
        {props.children}
      </div>
    )
    
    export default Layout

    server.js:

    const express = require('express')
    const next = require('next')
    
    const dev = process.env.NODE_ENV !== 'production'
    const app = next({ dev })
    const handle = app.getRequestHandler()
    
    app.prepare()
    .then(() => {
      const server = express()
    
      server.get('/p/:id', (req, res) => {
        const actualPage = '/post'
        const queryParams = { id: req.params.id } 
        app.render(req, res, actualPage, queryParams)
      })
    
      server.get('*', (req, res) => {
        return handle(req, res)
      })
    
      server.listen(3000, (err) => {
        if (err) throw err
        console.log('> Ready on http://localhost:3000')
      })
    })
    .catch((ex) => {
      console.error(ex.stack)
      process.exit(1)
    })

    package.json:

    {
      "name": "hello-next",
      "version": "1.0.0",
      "description": "",
      "main": "index.js",
      "scripts": {
        "test": "echo "Error: no test specified" && exit 1",
        "dev": "node server.js",
        "build": "next build",
        "start": "NODE_ENV=production node server.js"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "dependencies": {
        "express": "^4.16.4",
        "isomorphic-unfetch": "^3.0.0",
        "next": "^7.0.2",
        "react": "^16.6.3",
        "react-dom": "^16.6.3"
      }
    }
    
  • 相关阅读:
    linux学习笔记28--监控命令vmstat,iostat, lsof
    linux学习笔记27--监控命令ps和top,free
    linux学习笔记26--命令wc
    linux学习笔记24---命令grep
    linux学习笔记23--时间命令date和cal
    linux学习笔记22---命令diff和diff3
    linux学习笔记22--命令ln
    Fiddler 抓取 ios 端数据包
    fiddler 清除证书+重新添加证书
    Fiddler 详尽教程与抓取移动端数据包
  • 原文地址:https://www.cnblogs.com/xutongbao/p/11915733.html
Copyright © 2020-2023  润新知