Skip to content

[Doc EN] — history-mode.md — Add Node.js Native Usage #1596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jul 17, 2017
28 changes: 26 additions & 2 deletions docs/en/essentials/history-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,31 @@ location / {
}
```

#### Node.js (Express)
#### Native Node.js

```js
const http = require("http")
const fs = require("fs")
const httpPort = 80

http.createServer((req, res) => {
fs.readFile("index.htm", "utf-8", (err, content) => {
if (err) {
console.log('We cannot open "index.htm" file.')
}

res.writeHead(200, {
"Content-Type": "text/html; charset=utf-8"
})

res.end(content)
})
}).listen(httpPort, () => {
console.log("Server listening on: http://localhost:%s", httpPort)
})
```

#### Express with Node.js

For Node.js/Express, consider using [connect-history-api-fallback middleware](https://github.com/bripkens/connect-history-api-fallback).

Expand Down Expand Up @@ -95,4 +119,4 @@ const router = new VueRouter({
})
```

Alternatively, if you are using a Node.js server, you can implement the fallback by using the router on the server side to match the incoming URL and respond with 404 if no route is matched.
Alternatively, if you are using a Node.js server, you can implement the fallback by using the router on the server side to match the incoming URL and respond with 404 if no route is matched. Check out the [Vue server side rendering documentation](https://ssr.vuejs.org/en/) for more information.