Skip to content

Commit

Permalink
update docs for server.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
flybayer committed Apr 21, 2021
1 parent 287b7ba commit 2f576f6
Showing 1 changed file with 30 additions and 25 deletions.
55 changes: 30 additions & 25 deletions app/pages/docs/custom-server.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,23 @@ some other direct control over the web server itself.

### How {#how}

1. Add a `server.js` file in your project root. See below for an example.
1. Add a `server.ts` or `server.js` file in your project root. See below
for an example.
2. Now `blitz dev` and `blitz start` will automatically detect and use
your custom server.

### Example {#example}

Here's an example custom server:

```js
// server.js
const {createServer} = require("http")
const {parse} = require("url")
const blitz = require("blitz/custom-server")
```ts
// server.ts
import blitz from "blitz/custom-server"
import {createServer} from "http"
import {parse} from "url"
import {log} from "@blitzjs/display"

const {PORT = "3000"} = process.env
const dev = process.env.NODE_ENV !== "production"
const app = blitz({dev})
const handle = app.getRequestHandler()
Expand All @@ -30,34 +33,36 @@ app.prepare().then(() => {
createServer((req, res) => {
// Be sure to pass `true` as the second argument to `url.parse`.
// This tells it to parse the query portion of the URL.
const parsedUrl = parse(req.url, true)
const {pathname, query} = parsedUrl
const parsedUrl = parse(req.url!, true)
const {pathname} = parsedUrl

if (pathname === "/hello") {
res.writeHead(200).end("world")
return
}

if (pathname === "/a") {
if (pathname === "/hello") {
res.writeHead(200).end("world")
return
} else if (pathname === "/a") {
app.render(req, res, "/a", query)
} else if (pathname === "/b") {
app.render(req, res, "/b", query)
} else {
handle(req, res, parsedUrl)
}
}).listen(3000, (err) => {
if (err) throw err
console.log("> Ready on http://localhost:3000")

handle(req, res, parsedUrl)
}).listen(PORT, () => {
log.success(`Ready on http://localhost:${PORT}`)
})
})
```

> `server.js` doesn't go through babel or webpack. Make sure the syntax
> and sources this file requires are compatible with the current node
> version you are running.
---

The custom server uses the following import to connect the server with the
Blitz application:

The above `blitz` import is a function that receives an object with the
following options:
The above `blitz/custom-server` import is a function that receives an
object with the following options:

- `dev`: `Boolean` - Whether or not to launch Blitz in dev mode. Defaults
to `false`
Expand All @@ -72,10 +77,10 @@ required.

## Disabling file-system routing {#disabling-file-system-routing}

By default, `blitz` will serve each file in the `pages` folder under a
pathname matching the filename. If your project uses a custom server, this
behavior may result in the same content being served from multiple paths,
which can present problems with SEO and UX.
By default, `blitz` will serve each file `pages` folders under a pathname
matching the filename. If your project uses a custom server, this behavior
may result in the same content being served from multiple paths, which can
present problems with SEO and UX.

To disable this behavior and prevent routing based on files in `pages`,
open `blitz.config.js` and disable the `useFileSystemPublicRoutes` config:
Expand Down

0 comments on commit 2f576f6

Please sign in to comment.