summaryrefslogtreecommitdiff
path: root/examples/ssr/server/server.mjs
blob: d7a0a7a40f7723dde9452736891276c3d6f7dda7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { createServer } from 'http';
import fs from 'fs';
import mime from 'mime';
import { handler as ssrHandler } from '../dist/server/entry.mjs';

const clientRoot = new URL('../dist/client/', import.meta.url);

async function handle(req, res) {
	ssrHandler(req, res, async (err) => {
		if (err) {
			res.writeHead(500);
			res.end(err.stack);
			return;
		}

		let local = new URL('.' + req.url, clientRoot);
		try {
			const data = await fs.promises.readFile(local);
			res.writeHead(200, {
				'Content-Type': mime.getType(req.url),
			});
			res.end(data);
		} catch {
			res.writeHead(404);
			res.end();
		}
	});
}

const server = createServer((req, res) => {
	handle(req, res).catch((err) => {
		console.error(err);
		res.writeHead(500, {
			'Content-Type': 'text/plain',
		});
		res.end(err.toString());
	});
});

server.listen(8085);
console.log('Serving at http://localhost:8085');

// Silence weird <time> warning
console.error = () => {};
2022-04-10[ci] release (#3060)Gravatar github-actions[bot] 76-363/+288 Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> 2022-04-10update changesetGravatar Fred K. Schott 1-1/+0 2022-04-10update changesetGravatar Fred K. Schott 1-1/+1 2022-04-10update lockfile (#3059)Gravatar Fred K. Schott 29-914/+698 2022-04-10correct changesetsGravatar Fred K. Schott 3-3/+11 2022-04-10chore: webapi test now use chai (#3048)Gravatar Juan Martín Seery 16-814/+539 2022-04-10update test names containing spacesGravatar Fred K. Schott 26-17/+17 2022-04-11[ci] formatGravatar FredKSchott 2-8/+7 2022-04-10Fix SSR static build public file copying. fixes #3016 (#3037)Gravatar Steve Lee 8-5/+48 * Fix SSR static build public file copying * chore: update lockfile * remove dirname and use URL constructor * Cleanup test and actually test what it says it tests Co-authored-by: Nate Moore <nate@skypack.dev> 2022-04-10fix our weird beta release issues (#3031)Gravatar Fred K. Schott 4-6/+116 2022-04-10update tailwind integration readmeGravatar Fred K. Schott 2-9/+38 2022-04-10Fixed MD Lint issues (#3038)Gravatar Rafid Muhymin Wafi 1-12/+11