Node.js - Express special characters in routes (/campañas)

前端 未结 2 1911
情话喂你
情话喂你 2021-01-05 12:41

I have a problem trying to set a route in Node JS with Express framework.

My route is this one:

app.get(\'/campaña/nueva\', sms.nueva);
2条回答
  •  执念已碎
    2021-01-05 13:16

    I think you'll need to handle both a URL-encoded and perhaps a UTF-8 (and possibly Latin-1 also) variant. Check the following:

    1. How are your clients (browsers) sending the URL?

      • URL encoded as %C3%B1 ?
        • chrome and firefox send the %C3%B1 encoding
        • I would presume this is the dominant and compliant behavior
      • Unicode ?
        • I tested with curl and it looks to send a single character which I presume is just whatever encoding it got from my terminal, which is probably UTF-8.
    2. Based on that, try adjusting your route. You could use a regex or an explicit list

    .

    app.get('/campaña/nueva', sms.nueva)
    app.get('/campa%c3%b1a/nueva', sms.nueva)
    //Or for convenience if you like
    app.get('/' + encodeURIComponent('campaña') + '/nueva', sms.nueva) 
    

    My guess is ultimately most browsers are going to send the URL-encoded versions, so you can probably get by with just that last version.

提交回复
热议问题