File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change @@ -229,6 +229,26 @@ and then we edit the `package.json` file's start script to be
229
229
230
230
now run it with ` npm start `
231
231
232
+ ### Mocking a Static Backend
233
+ Webpack dev server allows us to modify the ExpressJS ` app ` object using the ` setup ` method.
234
+ We do this by passing a file to ` --server-config `
235
+
236
+ We create a file next to the projects ` package.json ` called ` server.conf.js ` with the content
237
+
238
+ ``` json
239
+ module.exports = function(app){
240
+ app.get('/api/*', function(req, res){
241
+ res.sendFile(req.originalUrl, {root: __dirname + '/mocks/'});
242
+ });
243
+ };
244
+
245
+ ```
246
+
247
+ now run it with ` ng serve --server-config server.conf.json `
248
+
249
+ Thus fetches to ` /api/foo ` will return flat json from ` /mocks/api/foo `
250
+
251
+
232
252
### Deploying the app via GitHub Pages
233
253
234
254
You can deploy your apps quickly via:
Original file line number Diff line number Diff line change
1
+ import { writeFile } from '../../utils/fs' ;
2
+ import { request } from '../../utils/http' ;
3
+ import { killAllProcesses , ng } from '../../utils/process' ;
4
+ import { ngServe } from '../../utils/project' ;
5
+ import { expectToFail } from '../../utils/utils' ;
6
+
7
+
8
+ export default function ( ) {
9
+ // const app = express();
10
+ const serverConfigFile = 'server.config.js' ;
11
+ const serverConfig = `module.exports = function(app){
12
+ app.get('/api/test', function(req, res){
13
+ res.json({ status:'TEST_API_RETURN' });
14
+ });
15
+ };` ;
16
+
17
+ return Promise . resolve ( )
18
+ . then ( ( ) => writeFile ( serverConfigFile , serverConfig ) )
19
+ . then ( ( ) => ngServe ( '--server-config' , serverConfigFile ) )
20
+ . then ( ( ) => request ( 'http://localhost:4200/api/test' ) )
21
+ . then ( body => {
22
+ if ( ! body . match ( / T E S T _ A P I _ R E T U R N / ) ) {
23
+ throw new Error ( 'Response does not match expected value.' ) ;
24
+ }
25
+ } )
26
+ . then ( ( ) => killAllProcesses ( ) , ( err ) => { killAllProcesses ( ) ; throw err ; } )
27
+
28
+ // A non-existing proxy file should error.
29
+ . then ( ( ) => expectToFail ( ( ) => ng ( 'serve' , '--server-config' , 'config.non-existent.js' ) ) ) ;
30
+ }
You can’t perform that action at this time.
0 commit comments