From 7c8cd4bc6920d0263a074a4789b9f8c251c905f7 Mon Sep 17 00:00:00 2001 From: fridays Date: Wed, 1 Mar 2017 18:42:47 +0100 Subject: [PATCH] Example with next-routes (#1290) * Example with next-routes * optimize description * rename to with-next-routes --- examples/with-next-routes/README.md | 29 ++++++++++++++++++++++++ examples/with-next-routes/package.json | 13 +++++++++++ examples/with-next-routes/pages/about.js | 1 + examples/with-next-routes/pages/blog.js | 26 +++++++++++++++++++++ examples/with-next-routes/pages/index.js | 11 +++++++++ examples/with-next-routes/routes.js | 5 ++++ examples/with-next-routes/server.js | 16 +++++++++++++ 7 files changed, 101 insertions(+) create mode 100644 examples/with-next-routes/README.md create mode 100644 examples/with-next-routes/package.json create mode 100644 examples/with-next-routes/pages/about.js create mode 100644 examples/with-next-routes/pages/blog.js create mode 100644 examples/with-next-routes/pages/index.js create mode 100644 examples/with-next-routes/routes.js create mode 100644 examples/with-next-routes/server.js diff --git a/examples/with-next-routes/README.md b/examples/with-next-routes/README.md new file mode 100644 index 00000000..60c22561 --- /dev/null +++ b/examples/with-next-routes/README.md @@ -0,0 +1,29 @@ +# Named routes example ([next-routes](https://github.com/fridays/next-routes)) + +## How to use + +Download the example [or clone the repo](https://github.com/zeit/next.js): + +```bash +curl https://codeload.github.com/zeit/next.js/tar.gz/master | tar -xz --strip=2 next.js-master/examples/with-next-routes +cd with-next-routes +``` + +Install it and run: + +```bash +npm install +npm run dev +``` + +Deploy it to the cloud with [now](https://zeit.co/now) ([download](https://zeit.co/download)) + +```bash +now +``` + +## The idea behind the example + +This example uses [next-routes](https://github.com/fridays/next-routes) for dynamic routing, which lets you define parameterized, named routes with express-style parameters matching. + +It works similar to the [parameterized-routing](https://github.com/zeit/next.js/tree/master/examples/parameterized-routing) example and makes use of next.js [custom server and routing](https://github.com/zeit/next.js#custom-server-and-routing) possibilities. diff --git a/examples/with-next-routes/package.json b/examples/with-next-routes/package.json new file mode 100644 index 00000000..2b552984 --- /dev/null +++ b/examples/with-next-routes/package.json @@ -0,0 +1,13 @@ +{ + "scripts": { + "dev": "node server.js", + "build": "next build", + "start": "NODE_ENV=production node server.js" + }, + "dependencies": { + "next": "^2.0.0-beta", + "next-routes": "^1.0.17", + "react": "^15.4.2", + "react-dom": "^15.4.2" + } +} diff --git a/examples/with-next-routes/pages/about.js b/examples/with-next-routes/pages/about.js new file mode 100644 index 00000000..1a4aab14 --- /dev/null +++ b/examples/with-next-routes/pages/about.js @@ -0,0 +1 @@ +export default props =>

About foo {props.url.query.foo}

diff --git a/examples/with-next-routes/pages/blog.js b/examples/with-next-routes/pages/blog.js new file mode 100644 index 00000000..12a3820f --- /dev/null +++ b/examples/with-next-routes/pages/blog.js @@ -0,0 +1,26 @@ +import React from 'react' + +const posts = [ + { slug: 'hello-world', title: 'Hello world' }, + { slug: 'another-blog-post', title: 'Another blog post' } +] + +export default class extends React.Component { + static async getInitialProps ({ query, res }) { + const post = posts.find(post => post.slug === query.slug) + + if (!post && res) { + res.statusCode = 404 + } + + return { post } + } + + render () { + const { post } = this.props + + if (!post) return

Post not found

+ + return

{post.title}

+ } +} diff --git a/examples/with-next-routes/pages/index.js b/examples/with-next-routes/pages/index.js new file mode 100644 index 00000000..4379efcc --- /dev/null +++ b/examples/with-next-routes/pages/index.js @@ -0,0 +1,11 @@ +import { Link, Router } from '../routes' + +export default () => ( + +) diff --git a/examples/with-next-routes/routes.js b/examples/with-next-routes/routes.js new file mode 100644 index 00000000..85f60858 --- /dev/null +++ b/examples/with-next-routes/routes.js @@ -0,0 +1,5 @@ +const nextRoutes = require('next-routes') +const routes = module.exports = nextRoutes() + +routes.add('blog', '/blog/:slug') +routes.add('about', '/about-us/:foo(bar|baz)') diff --git a/examples/with-next-routes/server.js b/examples/with-next-routes/server.js new file mode 100644 index 00000000..af1b0b20 --- /dev/null +++ b/examples/with-next-routes/server.js @@ -0,0 +1,16 @@ +const { createServer } = require('http') +const next = require('next') +const routes = require('./routes') + +const dev = process.env.NODE_ENV !== 'production' +const app = next({ dev }) +const handler = routes.getRequestHandler(app) + +app.prepare() +.then(() => { + createServer(handler) + .listen(3000, (err) => { + if (err) throw err + console.log('> Ready on http://localhost:3000') + }) +})