Introduction
- Routing is the core of the framework, located in the
src/routes
directory. By default, the routingpath
will be generated according to the directory structure - If the current route
method:'get'
, andsrc/pages
has 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 anapi
call, 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.js
page and passtitle
name
toprops
-
Visit http://localhost:3000/route/about because there is no corresponding file in the
pages
directory, so thejson
object is directly returned -
We modify
routes/route/index.js
as 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
post
requests. If you visit http://localhost:3000/route/index again, you will getdata is null
. Only settingpost
request 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
get
post
requests are allowed at the same time, the page will render normally, and you can also usefetch
current route in the page to getpost
data
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.js
default correspondingpath
is/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,
true
orfalse
turns nested layout mode on and off,array
sets custom layout, here View related documents
SSG
- Equal to
true
orfalse
turns static html generation on and off, here ViewSSG
related documents
render
- A string, if there is a corresponding file in the
src/pages
directory, the default is this file path, otherwise it isfalse
, for example: routeroutes/index.js
, if there is apages/index.js
file, 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.routes
can be set inndsk.config.js
for global configuration - You can See full documentation on the
route.options
configuration here