lwsjs / local-web-server

A lean, modular web server for rapid full-stack development.
MIT License
1.2k stars 85 forks source link

POST request No response #127

Closed rex-ll closed 4 years ago

rex-ll commented 4 years ago

Hi.

OS: Windows NT 10.0; Win64; x64 Node: 10.15.1 Local-web-server: 3.0.4

// lws.config.js
// -------------------------------------------
const config = require('./proxy.config');

const rewrite = config.proxyContext.map(url => {
    return {
        from: `${url}/(.*)`,
        to: `${config.target}${url}/$1`
    }
}).concat([{
    from: '/pages/(.*)',
    to: '/$1',
}]);
module.exports = {
    spa: 'index.html',
    port: 3000,
    rewrite,
    directory: 'build',
    logFormat: 'dev',
    open: true,
    verbose: false,
    corsCredentials: true
};

No problem sending GET requests,When sending a POST request:

 Middleware error
Error: read ECONNRESET
    at TLSWrap.onStreamRead

thanks!

rex-ll commented 4 years ago

image

Postman test shows that it is not a proxy problem.

75lb commented 4 years ago

Hi.. i need more info to help me reproduce this issue.

Could you run this in your project folder and send me the output, please.

$ ws --config

Also, please send me the --verbose output. So, run ws --verbose, reproduce your issue then send me the output.

Thank.s

rex-ll commented 4 years ago

$ ws --config

{
    port: 3000,
    moduleDir: [
        '.',
        'C:\\project\\React\\wechat-admin-a\\node_modules\\local-web-server'
    ],
    modulePrefix: 'lws-',
    stack:
        MiddlewareStack [
            BasicAuth,
                BodyParser,
                RequestMonitor,
                Log,
                Cors,
                Json,
                Compress,
                Rewrite,
                Blacklist,
                ConditionalGet,
                Mime,
                Range,
                SPA,
                Static,
                Index
            ],
    spa: 'index.html',
    hostname: 'localhost',
    rewrite: [
        {
            from: '/News/(.*)', 
            to: 'http://10.20.10.98:8090/News/$1'
        },
        {
            from: '/Account/(.*)',
            to: 'http://10.20.10.98:8090/Account/$1'
        },
        {
            from: '/UpdateFile/(.*)',
            to: 'http://10.20.10.98:8090/UpdateFile/$1'
        },
        {
            from: '/DepartMent/(.*)',
            to: 'http://10.20.10.98:8090/DepartMent/$1'
        },
        {
            from: '/Project/(.*)',
            to: 'http://10.20.10.98:8090/Project/$1'
        },
        {
            from: '/pages/(.*)', 
            to: '/$1'
        }],
    directory: 'build',
    logFormat: 'dev',
    open: true,
    verbose: true,
    corsCredentials: true,
    view: CliView{}
}

Here is a normal GET request log (verbose===true)

   { bytesRead: '0', bytesWritten: '0', remoteAddress: '127.0.0.1' } }
{ 'server.socket.data':
   { bytesRead: '677 B',
     bytesWritten: '0',
     remoteAddress: '127.0.0.1' } }
{ 'server.request':
   { requestId: 21,
     method: 'GET',
     url: '/Account/GetCode?code=1563412696851',
     headers:
      { host: 'localhost:3000',
        connection: 'keep-alive',
        'user-agent':
         'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
        dnt: '1',
        accept: 'image/webp,image/apng,image/*,*/*;q=0.8',
        referer: 'http://localhost:3000/pages/login',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8',
        cookie:
         'Webstorm-462d154e=f0a2e046-bd39-4586-9954-602e64b379f6; .AspNetCore.Session=CfDJ8NYUT9ApYm5JnNmz4l77be6iuHvdgFRRVs4TZqXzm7rPySqEPZyJjUdhk0zB5dt0rAcxI0SKqxY79m9HGi82u6rbfiwMQpb9%2Fju2N0%2FuJhwrcE9IIZcJnF6tJuE0lcb6UfsAyQtbU
wZVy0aUeO4YLSWr1iKpagxUG8ny%2FS3pkhcq' } } }
{ 'middleware.rewrite.remote.request':
   { rewrite:
      { id: 4,
        from: '/Account/GetCode?code=1563412696851',
        to: 'http://10.20.10.98:8090/Account/GetCode?code=1563412696851' },
     method: 'GET',
     headers:
      { host: '10.20.10.98:8090',
        connection: 'keep-alive',
        'user-agent':
         'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
        dnt: '1',
        accept: 'image/webp,image/apng,image/*,*/*;q=0.8',
        referer: 'http://localhost:3000/pages/login',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8',
        cookie:
         'Webstorm-462d154e=f0a2e046-bd39-4586-9954-602e64b379f6; .AspNetCore.Session=CfDJ8NYUT9ApYm5JnNmz4l77be6iuHvdgFRRVs4TZqXzm7rPySqEPZyJjUdhk0zB5dt0rAcxI0SKqxY79m9HGi82u6rbfiwMQpb9%2Fju2N0%2FuJhwrcE9IIZcJnF6tJuE0lcb6UfsAyQtbU
wZVy0aUeO4YLSWr1iKpagxUG8ny%2FS3pkhcq' } } }
{ 'middleware.rewrite.remote.response':
   { rewrite:
      { id: 4,
        from: '/Account/GetCode?code=1563412696851',
        to: 'http://10.20.10.98:8090/Account/GetCode?code=1563412696851' },
     status: 200,
     headers:
      { 'content-type': 'image/jpeg',
        server: 'Microsoft-IIS/10.0',
        'x-powered-by': 'ASP.NET',
        date: 'Thu, 18 Jul 2019 01:20:16 GMT',
        'content-length': '1999' } } }
{ 'server.response':
   { requestId: 21,
     statusCode: 200,
     headers:
      [Object: null prototype] {
        vary: 'Origin',
        'content-type': 'image/jpeg',
        server: 'Microsoft-IIS/10.0',
        'x-powered-by': 'ASP.NET',
        date: 'Thu, 18 Jul 2019 01:20:16 GMT',
        'content-length': '1999' } } }
GET /Account/GetCode?code=1563412696851 200 11.901 ms - 1999
{ 'server.socket.new':
   { bytesRead: '0', bytesWritten: '0', remoteAddress: '127.0.0.1' } }
{ 'server.socket.timeout':
   { bytesRead: '677 B',
     bytesWritten: '2.2 kB',
     remoteAddress: '127.0.0.1' } }
{ 'server.socket.close':
   { bytesRead: '677 B',
     bytesWritten: '2.2 kB',
     remoteAddress: '127.0.0.1' } }
{ 'process.memoryUsage':
   { rss: '35.2 MB',
     heapTotal: '18.1 MB',
     heapUsed: '13.8 MB',
     external: '97.8 kB' } }

But when sending POST requests,until request timeout:

{ 'server.socket.new':
   { bytesRead: '0', bytesWritten: '0', remoteAddress: '127.0.0.1' } }
{ 'server.socket.data':
   { bytesRead: '808 B',
     bytesWritten: '0',
     remoteAddress: '127.0.0.1' } }
{ 'server.request':
   { requestId: 22,
     method: 'POST',
     url: '/Account/Login',
     headers:
      { host: 'localhost:3000',
        connection: 'keep-alive',
        'content-length': '53',
        accept: 'application/json, text/plain, */*',
        origin: 'http://localhost:3000',
        'user-agent':
         'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
        dnt: '1',
        'content-type': 'application/x-www-form-urlencoded',
        referer: 'http://localhost:3000/pages/login',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8',
        cookie:
         'Webstorm-462d154e=f0a2e046-bd39-4586-9954-602e64b379f6; .AspNetCore.Session=CfDJ8NYUT9ApYm5JnNmz4l77be6iuHvdgFRRVs4TZqXzm7rPySqEPZyJjUdhk0zB5dt0rAcxI0SKqxY79m9HGi82u6rbfiwMQpb9%2Fju2N0%2FuJhwrcE9IIZcJnF6tJuE0lcb6UfsAyQtbU
wZVy0aUeO4YLSWr1iKpagxUG8ny%2FS3pkhcq' },
     body: { UserName: 'admin', UserPwd: '******', Vcode: 'bmwje' } } }
{ 'middleware.rewrite.remote.request':
   { rewrite:
      { id: 5,
        from: '/Account/Login',
        to: 'http://10.20.10.98:8090/Account/Login' },
     method: 'POST',
     headers:
      { host: '10.20.10.98:8090',
        connection: 'keep-alive',
        'content-length': '53',
        accept: 'application/json, text/plain, */*',
        origin: 'http://localhost:3000',
        'user-agent':
         'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36',
        dnt: '1',
        'content-type': 'application/x-www-form-urlencoded',
        referer: 'http://localhost:3000/pages/login',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'zh-CN,zh;q=0.9,zh-TW;q=0.8',
        cookie:
         'Webstorm-462d154e=f0a2e046-bd39-4586-9954-602e64b379f6; .AspNetCore.Session=CfDJ8NYUT9ApYm5JnNmz4l77be6iuHvdgFRRVs4TZqXzm7rPySqEPZyJjUdhk0zB5dt0rAcxI0SKqxY79m9HGi82u6rbfiwMQpb9%2Fju2N0%2FuJhwrcE9IIZcJnF6tJuE0lcb6UfsAyQtbU
wZVy0aUeO4YLSWr1iKpagxUG8ny%2FS3pkhcq' } } }
{ 'server.socket.new':
   { bytesRead: '0', bytesWritten: '0', remoteAddress: '127.0.0.1' } }
{ 'server.socket.end':
   { bytesRead: '0', bytesWritten: '0', remoteAddress: '127.0.0.1' } }
{ 'server.socket.close':
   { bytesRead: '0', bytesWritten: '0', remoteAddress: '127.0.0.1' } }
{ 'process.memoryUsage':
   { rss: '35.4 MB',
     heapTotal: '18.1 MB',
     heapUsed: '14.0 MB',
     external: '173.4 kB' } }
{ 'process.memoryUsage':
   { rss: '35.4 MB',
     heapTotal: '18.1 MB',
     heapUsed: '14.0 MB',
     external: '173.4 kB' } }
{ 'server.socket.timeout':
   { bytesRead: '808 B',
     bytesWritten: '0',
     remoteAddress: '127.0.0.1' } }
{ 'server.socket.close':
   { bytesRead: '808 B',
     bytesWritten: '0',
     remoteAddress: '127.0.0.1' } }
POST /Account/Login - - ms - -

09:23:51: Middleware error
Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)

I'm sorry the code is a bit long. Thank you very much.

75lb commented 4 years ago

thanks, I will try and reproduce this issue today..

75lb commented 4 years ago

Postman test shows that it is not a proxy problem.

In this Postman example which works correctly, could you show me the headers that are sent please.

I want to compare the headers sent by Postman with the headers sent by lws-rewrite. Thanks.

wywywywy commented 4 years ago

Same problem here.

POST with no data and no additional headers = OK.

POST with json data and "Content-Type: application/json" = No response

Going back to version 2.6.1 makes it work fine again.

75lb commented 4 years ago

I am unable to reproduce this issue locally but i expect this long-standing request issue is to blame.

Local-web-server v3 introduced support for HTTP Proxies (e.g. your corporate web proxy). To achieve this it switched to using the request module as it supports corporate HTTP proxies out of the box. However, it looks like bringing in request has also introduced issues like this one!

Will fix ASAP as a matter of priority.

rex-ll commented 4 years ago

In this Postman example which works correctly, could you show me the headers that are sent please.

I want to compare the headers sent by Postman with the headers sent by lws-rewrite. Thanks.

image hello, This is a request sent using the WS proxy service.

I've actually tried to compare the information in headers, but I haven't found any problems.

75lb commented 4 years ago

Fixed in lws-rewrite v2.0.6. Please reinstall local-web-server and verify whether it now works correctly for you. Thanks.

gerardcarbo commented 4 years ago

In my case after reinstalling:

$ ws --version
3.0.6

Still with no response and using lws-rewrite v.1.1, here the logs:

POST /server/computeSaleTotals - - ms - -
{ 'server.socket.data':
   { bytesRead: '1.5 kB',
     bytesWritten: '0',
     remoteAddress: '::ffff:127.0.0.1' } }
{ 'server.request':
   { requestId: 67,
     method: 'POST',
     url: '/server/computeSaleTotals',
     headers:
      { host: '127.0.0.1:8000',
        connection: 'keep-alive',
        'content-length': '482',
        accept: 'application/json, text/plain, */*',
        origin: 'http://127.0.0.1:8000',
        authorization:
         'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ1bmFtZSI6ImFkbWluIiwidWlkIjoiMSIsImV4cCI6MTU2Mzk1OTI3My4wNTA2OTIzLCJ0b3V0IjozMDAsInByaXZpbGVnZXMiOlsiMSIsIjIiLCIzIiwiNCIsIjUiLCI2IiwiYWRtaW4iXSwidXJvbGUiOiJhZG1pbiJ9.TWbMcBT51sKEgqkJJ5Dun56y_h0t0aaoZ0_Wl7mF1K3SvEJWT1G9p8hKDlhj6bzG3_IXfo5ofOF_bft6_Dj_1A',
        'user-agent':
         'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
        'content-type': 'application/json',
        referer: 'http://127.0.0.1:8000/pos/family/99',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9,es;q=0.8,ca;q=0.7',
        cookie:
         'client_rate=1; _nc=VxLlpJ916R%252bKp7D8j3rfn1IFAz53CpDMWK4%252bNLroAAs%253dhRAyQAeCsZOTSF7FjHegYcUoTg4Pi4Ey4a9U4oN5w0GnjKWeKKFAYi04POY9shrBNp3Wq6uQjgfGrAQz0E3%252f8MP2gxmIhEj5FGBQP4b9%252beUlBD0%252ba4k8xub4nF3PHgxD; __NCTRACE=8a234d7b-08bb-49f2-b69f-fceb7ddc712c' },
     body:
      { articles: {},
        totals: { basis: 0, tax: 0, surcharge: 0, total: 0, retention: 0 },
        discount100: 0,
        client:
         { id: 1,
           name: 'VARIS CLIENT BOTIGA',
           trade_mark: '',
           email: '.',
           postal_code: '.',
           invoicing_client_id: 1,
           fiscal_id: '-',
           address: '.',
           address2: '',
           city: '.',
           state: '.',
           country: '.',
           phone1: '.',
           phone2: '',
           discount100: 0,
           rate_id: 1,
           surcharge: false,
           intracom_client: false,
           retention100: 0,
           deferment_of_payment: 0,
           day_of_payment: 0,
           sale_spot: true,
           serie_id: 1 } } } }
{ 'middleware.rewrite.remote.request':
   { rewrite:
      { id: 7,
        from: '/server/computeSaleTotals',
        to: 'http://localhost:8080/server/computeSaleTotals' },
     method: 'POST',
     headers:
      { host: 'localhost:8080',
        'content-length': '482',
        accept: 'application/json, text/plain, */*',
        origin: 'http://127.0.0.1:8000',
        authorization:
         'Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzUxMiJ9.eyJ1bmFtZSI6ImFkbWluIiwidWlkIjoiMSIsImV4cCI6MTU2Mzk1OTI3My4wNTA2OTIzLCJ0b3V0IjozMDAsInByaXZpbGVnZXMiOlsiMSIsIjIiLCIzIiwiNCIsIjUiLCI2IiwiYWRtaW4iXSwidXJvbGUiOiJhZG1pbiJ9.TWbMcBT51sKEgqkJJ5Dun56y_h0t0aaoZ0_Wl7mF1K3SvEJWT1G9p8hKDlhj6bzG3_IXfo5ofOF_bft6_Dj_1A',
        'user-agent':
         'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36',
        'content-type': 'application/json',
        referer: 'http://127.0.0.1:8000/pos/family/99',
        'accept-encoding': 'gzip, deflate, br',
        'accept-language': 'en-US,en;q=0.9,es;q=0.8,ca;q=0.7',
        cookie:
         'client_rate=1; _nc=VxLlpJ916R%252bKp7D8j3rfn1IFAz53CpDMWK4%252bNLroAAs%253dhRAyQAeCsZOTSF7FjHegYcUoTg4Pi4Ey4a9U4oN5w0GnjKWeKKFAYi04POY9shrBNp3Wq6uQjgfGrAQz0E3%252f8MP2gxmIhEj5FGBQP4b9%252beUlBD0%252ba4k8xub4nF3PHgxD; __NCTRACE=8a234d7b-08bb-49f2-b69f-fceb7ddc712c',
        via: '1.1 lws-rewrite' } } }
11:05:02: Middleware error
Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
75lb commented 4 years ago

thanks for the detailed report, I've finally reproduced this - it's related to koa-bodyparser.. A fix is incoming.

75lb commented 4 years ago

As a temporary fix, add this stack to your lws.config.js file - it is the default stack but with lws-body-parser removed. Unless you have a custom middleware in your stack which reads ctx.request.body this should work fine. You can safely remove any middleware (e.g. blacklist) from this list if you're not using them.

module.exports = {
  stack: [
    'lws-basic-auth',
    'lws-request-monitor',
    'lws-log',
    'lws-cors',
    'lws-json',
    'lws-compress',
    'lws-rewrite',
    'lws-blacklist',
    'lws-conditional-get',
    'lws-mime',
    'lws-range',
    'lws-spa',
    'lws-static',
    'lws-index'
  ]
}

Or, you could use this command - it also omits body-parser.

$ ws --stack basic-auth request-monitor log cors json compress rewrite blacklist conditional-get mime range spa static index

Meanwhile I'll work on a permanent fix.

75lb commented 4 years ago

Fixed and released in local-web-server v3.0.7. Please reinstall and try again.

lws-body-parser was consuming the incoming request body stream before lws-rewrite had chance to pipe it to the remote target. Thanks again for the issue reports, let me know if you find anything else.

gerardcarbo commented 4 years ago

Working like a charm! Thanks for the support and for the great project.

rex-ll commented 4 years ago

Thank you very much for your bug fix.