MCPcopy Create free account
hub / github.com/TanStack/router / createServer

Function createServer

examples/react/basic-ssr-file-based/server.js:8–96  ·  view source on GitHub ↗
(
  root = process.cwd(),
  isProd = process.env.NODE_ENV === 'production',
  hmrPort,
)

Source from the content-addressed store, hash-verified

6const isTest = process.env.NODE_ENV === 'test' || !!process.env.VITE_TEST_BUILD
7
8export async function createServer(
9 root = process.cwd(),
10 isProd = process.env.NODE_ENV === 'production',
11 hmrPort,
12) {
13 const app = express()
14
15 /**
16 * @type {import('vite').ViteDevServer}
17 */
18 let vite
19 if (!isProd) {
20 vite = await (
21 await import('vite')
22 ).createServer({
23 root,
24 logLevel: isTest ? 'error' : 'info',
25 server: {
26 middlewareMode: true,
27 watch: {
28 // During tests we edit the files too fast and sometimes chokidar
29 // misses change events, so enforce polling for consistency
30 usePolling: true,
31 interval: 100,
32 },
33 hmr: {
34 port: hmrPort,
35 },
36 },
37 appType: 'custom',
38 })
39 // use vite's connect instance as middleware
40 app.use(vite.middlewares)
41 } else {
42 app.use(
43 (await import('compression')).default({
44 brotli: {
45 flush: zlib.constants.BROTLI_OPERATION_FLUSH,
46 },
47 flush: zlib.constants.Z_SYNC_FLUSH,
48 }),
49 )
50 }
51
52 if (isProd) app.use(express.static('./dist/client'))
53
54 app.use('/{*splat}', async (req, res) => {
55 try {
56 const url = req.originalUrl
57
58 if (path.extname(url) !== '') {
59 console.warn(`${url} is not valid router path`)
60 res.status(404)
61 res.end(`${url} is not valid router path`)
62 return
63 }
64
65 // Best effort extraction of the head from vite's index transformation hook

Callers 3

server.jsFile · 0.70
getAvailablePortFunction · 0.50
canListenFunction · 0.50

Calls 4

warnMethod · 0.65
endMethod · 0.65
infoMethod · 0.65
renderMethod · 0.45

Tested by 2

getAvailablePortFunction · 0.40
canListenFunction · 0.40