Introduction
- Routing is the core of the framework, located in the
src/routesdirectory. By default, the routingpathwill be generated according to the directory structure - If the current route
method:'get', andsrc/pageshas a directory corresponding to the route, it will be used as the routing configuration file ofpages, rendering and passing data to the page, otherwise it will be used as anapicall, as shown in the following example:
my-nodestack-app
├── src
│ ├── pages
│ │ │── route
│ │ | │── index.js
│ ├── routes
│ │ │── route
│ │ | │── index.js
│ │ | │── about.js
├── ndsk.config.js
└── package.json
export default (props)=>{
return (
<html>
<head>
<title>{props.title}</title>
</head>
<body>
{props.name || 'data is null'}
</body>
</html>
)
}
export default (request)=>{
return {
title:'route',
name:'this is route index page'
}
}
export default (request)=>{
return {
title:'route',
name:'this is route about page'
}
}
-
Visit http://localhost:3000/route/index to render the
pages/route/index.jspage and passtitlenametoprops -
Visit http://localhost:3000/route/about because there is no corresponding file in the
pagesdirectory, so thejsonobject is directly returned -
We modify
routes/route/index.jsas follows:
export const method = ['post']
export default (request)=>{
return {
title:'route',
name:'this is route index page'
}
}
- The above indicates that the current route only allows
postrequests. If you visit http://localhost:3000/route/index again, you will getdata is null. Only settingpostrequest will not render the page. - Modify again as follows:
export const method = ['get','post']
export default (request)=>{
return {
title:'route',
name:'this is route index page'
}
}
- The above indicates that
getpostrequests are allowed at the same time, the page will render normally, and you can also usefetchcurrent route in the page to getpostdata
export const method = ['get','post']
export default ({method})=>{
return {
title:'route',
name:`this is ${method} request`
}
}
import {useState} from 'react'
export default (props)=>{
const [value,setValue] = useState(props.name);
const onClick = ()=>{
fetch('/route',{
method:"POST"
}).then(async text=>{
const {name} = await text.json();
setValue(name)
})
}
return (
<html>
<head>
<title>{props.title}</title>
</head>
<body>
<div>{value}</div>
<button onClick={onClick}>Post</button>
</body>
</html>
)
}
Routing Configuration
export const method = ['get'];
export const path = '/example';
export const layout = true;
export const SSG = false;
export const render = false;
export const options = {};
export default = (request)=>{
return {}
}
method
- Allowed request types, can be
['get'],['post'],['get','post'], default value is['get']
path
- Routing path, must start with
/, by default generated according to the directory structure, for example,src/routes/test.jsdefault correspondingpathis/test, you can allow custom configuration path, for example:
export const path = '/book/{id}' //All /book/aaa /book/bbb /book/xxx will match the current route
export const path = '/book/{id}/cover' //All /book/aaa/cover /book/bbb/cover /book/xxx/cover will match the current route
export const path = '/book/{id}*' //All URLs starting with /book/ will match the current route
export const path = '/{p}*' //Match all routes
export const path = '/book/{id?}' //? indicates an optional parameter, /book/ /book/test /book/a /book/1 all point to the current route
export const path = '/book{id?}/app' // /book1/app /book2/app /booktest/app all point to the current route
layout
- Whether to enable nested layout,
trueorfalseturns nested layout mode on and off,arraysets custom layout, here View related documents
SSG
- Equal to
trueorfalseturns static html generation on and off, here ViewSSGrelated documents
render
- A string, if there is a corresponding file in the
src/pagesdirectory, the default is this file path, otherwise it isfalse, for example: routeroutes/index.js, if there is apages/index.jsfile, it is/index.js, you can change it to render other page files
options
- Routing configuration options, each route can be configured separately, or
server.routescan be set inndsk.config.jsfor global configuration - You can See full documentation on the
route.optionsconfiguration here