|
1 |
| -import { Server } from 'azle'; |
2 | 1 | import express, { Request, Response } from 'express';
|
3 | 2 | import { writeFileSync, createReadStream } from 'fs';
|
4 | 3 |
|
5 | 4 | let globalState = {};
|
6 | 5 |
|
7 |
| -export default Server(() => { |
8 |
| - writeFileSync( |
9 |
| - '/dist/test.html', |
10 |
| - `<!DOCTYPE html><html><body>HTML from the filesystem</body></html>` |
11 |
| - ); |
| 6 | +writeFileSync( |
| 7 | + '/dist/test.html', |
| 8 | + `<!DOCTYPE html><html><body>HTML from the filesystem</body></html>` |
| 9 | +); |
12 | 10 |
|
13 |
| - writeFileSync('/dist/test.txt', 'I have written some text to this file'); |
| 11 | +writeFileSync('/dist/test.txt', 'I have written some text to this file'); |
14 | 12 |
|
15 |
| - writeFileSync('/dist/send-file.txt', 'Does this work too?'); |
| 13 | +writeFileSync('/dist/send-file.txt', 'Does this work too?'); |
16 | 14 |
|
17 |
| - const app = express(); |
| 15 | +const app = express(); |
18 | 16 |
|
19 |
| - app.get('/res-send', (req, res) => { |
20 |
| - res.send('Just testing res.send'); |
21 |
| - }); |
| 17 | +app.get('/res-send', (req, res) => { |
| 18 | + res.send('Just testing res.send'); |
| 19 | +}); |
22 | 20 |
|
23 |
| - app.get('/res-write', (req, res) => { |
24 |
| - res.write('Why hello there sir'); |
25 |
| - res.end(); |
26 |
| - }); |
| 21 | +app.get('/res-write', (req, res) => { |
| 22 | + res.write('Why hello there sir'); |
| 23 | + res.end(); |
| 24 | +}); |
27 | 25 |
|
28 |
| - app.get('/file-stream', (req, res) => { |
29 |
| - const fileStream = createReadStream('/dist/test.txt'); |
| 26 | +app.get('/file-stream', (req, res) => { |
| 27 | + const fileStream = createReadStream('/dist/test.txt'); |
30 | 28 |
|
31 |
| - fileStream.pipe(res); |
32 |
| - }); |
| 29 | + fileStream.pipe(res); |
| 30 | +}); |
33 | 31 |
|
34 |
| - app.get('/global-state', (req, res) => { |
35 |
| - res.json(globalState); |
36 |
| - }); |
| 32 | +app.get('/global-state', (req, res) => { |
| 33 | + res.json(globalState); |
| 34 | +}); |
37 | 35 |
|
38 |
| - app.get('/500', (req, res) => { |
39 |
| - res.sendStatus(500); |
40 |
| - }); |
| 36 | +app.get('/500', (req, res) => { |
| 37 | + res.sendStatus(500); |
| 38 | +}); |
41 | 39 |
|
42 |
| - app.get('/send-file', (req, res) => { |
43 |
| - res.sendFile('/dist/send-file.txt'); |
44 |
| - }); |
| 40 | +app.get('/send-file', (req, res) => { |
| 41 | + res.sendFile('/dist/send-file.txt'); |
| 42 | +}); |
45 | 43 |
|
46 |
| - app.use(express.json()); |
| 44 | +app.use(express.json()); |
47 | 45 |
|
48 |
| - app.post('/global-state/post', changeGlobalState); |
49 |
| - app.put('/global-state/put', changeGlobalState); |
50 |
| - app.patch('/global-state/patch', changeGlobalState); |
| 46 | +app.post('/global-state/post', changeGlobalState); |
| 47 | +app.put('/global-state/put', changeGlobalState); |
| 48 | +app.patch('/global-state/patch', changeGlobalState); |
51 | 49 |
|
52 |
| - app.delete('/global-state/delete', (req, res) => { |
53 |
| - globalState = {}; |
| 50 | +app.delete('/global-state/delete', (req, res) => { |
| 51 | + globalState = {}; |
54 | 52 |
|
55 |
| - res.json(globalState); |
56 |
| - }); |
| 53 | + res.json(globalState); |
| 54 | +}); |
57 | 55 |
|
58 |
| - const router = express.Router(); |
| 56 | +const router = express.Router(); |
59 | 57 |
|
60 |
| - router.get('/user/:id', (req, res) => { |
61 |
| - res.send(req.params.id); |
62 |
| - }); |
| 58 | +router.get('/user/:id', (req, res) => { |
| 59 | + res.send(req.params.id); |
| 60 | +}); |
63 | 61 |
|
64 |
| - router.get('/post', (req, res) => { |
65 |
| - res.send(req.query.id); |
66 |
| - }); |
| 62 | +router.get('/post', (req, res) => { |
| 63 | + res.send(req.query.id); |
| 64 | +}); |
67 | 65 |
|
68 |
| - app.use('/router', router); |
| 66 | +app.use('/router', router); |
69 | 67 |
|
70 |
| - app.use(express.static('/dist')); |
| 68 | +app.use(express.static('/dist')); |
71 | 69 |
|
72 |
| - return app.listen(); |
73 |
| -}); |
| 70 | +app.listen(); |
74 | 71 |
|
75 | 72 | function changeGlobalState(req: Request, res: Response) {
|
76 | 73 | globalState = req.body;
|
|
0 commit comments