express#$Application JavaScript Examples

The following examples show how to use express#$Application. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: mox-test-server.js    From mox with MIT License 5 votes vote down vote up
initTestServer = (
  port: number = 3100,
  name: string = 'default_server'
): Promise<http.Server> => {
  return new Promise<http.Server>(resolve => {
    const app: $Application<> = express();

    app.get('/*/array', (req: $Request, res: $Response) => {
      res.send(['foo', 'bar', 'baz']);
    });

    app.get('/*/object', (req: $Request, res: $Response) => {
      res.send({
        id: 'zxcv',
        name: 'Bob',
        location: 'Palo Alto, CA',
      });
    });

    app.get('/*/country/*/state/*/info', (req: $Request, res: $Response) => {
      const [, country, state] =
        req.originalUrl.match(/country\/([^/]+)\/state\/([^/]+)\/info/) ?? [];

      res.send({
        country,
        state,
      });
    });

    app.get('/*/info', (req: $Request, res: $Response) => {
      res.send({ name, port });
    });

    app.get('/*/send-back-header/:value', (req: $Request, res: $Response) => {
      const incHeader = req.header('x-mox-incoming-test');
      res.setHeader('x-mox-outgoing-test', req.params.value);
      res.send({ received: incHeader ?? null });
    });

    app.get('/*/text-plain-looks-like-json', (req: $Request, res: $Response) => {
      res.type('text/plain').send(JSON.stringify({ message: 'looks like json' }));
    });

    app.post('/*/send-back-foo', async (req: $Request, res: $Response) => {
      const contentType = req.get('content-type');
      if (contentType !== 'application/json') {
        res.status(400);
        res.send('Incorrect content type');
      } else {
        await new Promise(resolve => bodyParser.json()(req, null, resolve));
        res.type('application/json');
        res.send({ 'this-is-foo': (req.body: any).foo });
      }
    }