angular / universal-starter

Angular Universal starter kit by @AngularClass
2.02k stars 688 forks source link

Is there any way of doing a post request WITHOUT javascript ? #685

Open Avcajaraville opened 5 years ago

Avcajaraville commented 5 years ago

A use case I have for server side rendering are clients that, for some reason, don't have javascript available.

All get requests work fine in SSR, but Im not able to make a post request using SSR.

As an example, say I have these routes on express:

app.post('/api/login', (req, res, next) => {
  res.locals.login = true;
  next();
});

app.get('*', (req, res) => {
  res.render(
    'index',
    {
      req,
      res,
      providers: [
        {
          provide: 'REQUEST',
          useValue: req,
        },
        {
          provide: 'RESPONSE',
          useValue: res,
        },
      ],
    },
    (err, html) => {
      console.log(`GET: ${req.originalUrl}`);
      if (!!err) throw err;
      res.send(html);
    }
  );
});

I have my components injected with the RESPONSE service, and accessing it like:

import { RESPONSE } from '@nguniversal/express-engine/tokens';

constructor(@Inject(RESPONSE) private response: any) {
  this.response.locals // undefined
}

Any way I can access the res.locals on my angular component via server side when performing a classic POST request ?

Im using a form element, with action and post correctly set, code is just orientative.

Im afraid this might not be posible without an ajax request. Is this the case?