Tsed: A 404 response when requesting a static file

Created on 4 Jul 2018  Β·  7Comments  Β·  Source: tsedio/tsed

Informations

Type | Ts.Ed Version | Typescript Version | Node Version
---|---|--|---
Bug | v3.10.2 | v2.7.2 | v8.11.1


Description

Hi, Romakita

I have a problem getting serveStatic config to work as I always receive a 404 response when requesting a static file. Here is my config:

/home/mouneer/ts-server/src/app.ts

@ServerSettings({
  serveStatic: {
    ['assets']: path.resolve(__dirname, '../assets'),
  },
})

File structure:

ts-server
β”‚   ...    
└───src
β”‚   β”‚   app.ts
β”‚   ...    
└───dist
β”‚   β”‚   app.js
β”‚   ...    
└───assets
β”‚   β”‚   ...
β”‚   └───profile
β”‚       β”‚   avatar.png

During Initiation:

[2018-07-04T16:12:55.404] [INFO ] [TSED] - settings.serveStatic => { '/assets': '/home/mouneer/ts-server/assets' }

Request Log:

[2018-07-04T16:16:41.356] [DEBUG] [TSED] - {
  "event": "start",
  "reqId": "3",
  "method": "GET",
  "url": "/assets/profile/avatar.png",
  "duration": 0,
  "headers": {
    "host": "localhost:3020",
    "connection": "close",
    "cache-control": "max-age=0",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9",
    "cookie": "connect.sid=s%3APwaFQ8_7wVBQDuNVh64lf0KWD0hA7W2Z.ZFw7AzNuza4NAKU08O9hH%2FDV%2FUCpA8N2Va94Zl8v1Uw"
  },
  "body": {},
  "query": {},
  "params": {}
}
[2018-07-04T16:16:41.356] [DEBUG] [TSED] - {
  "type": "middleware",
  "target": "LogIncomingRequestMiddleware",
  "methodName": "use",
  "injectable": true,
  "event": "invoke.end",
  "reqId": "3",
  "method": "GET",
  "url": "/assets/profile/avatar.png",
  "duration": 0,
  "headers": {
    "host": "localhost:3020",
    "connection": "close",
    "cache-control": "max-age=0",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9",
    "cookie": "connect.sid=s%3APwaFQ8_7wVBQDuNVh64lf0KWD0hA7W2Z.ZFw7AzNuza4NAKU08O9hH%2FDV%2FUCpA8N2Va94Zl8v1Uw"
  },
  "body": {},
  "query": {},
  "params": {}
}
[2018-07-04T16:16:41.356] [INFO ] [TSED] - {
  "status": 404,
  "reqId": "3",
  "method": "GET",
  "url": "/assets/profile/avatar.png",
  "duration": 0
}
[2018-07-04T16:16:41.356] [DEBUG] [TSED] - {
  "status": 404,
  "reqId": "3",
  "method": "GET",
  "url": "/assets/profile/avatar.png",
  "duration": 0,
  "headers": {
    "host": "localhost:3020",
    "connection": "close",
    "cache-control": "max-age=0",
    "upgrade-insecure-requests": "1",
    "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36",
    "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
    "accept-encoding": "gzip, deflate, br",
    "accept-language": "en-US,en;q=0.9",
    "cookie": "connect.sid=s%3APwaFQ8_7wVBQDuNVh64lf0KWD0hA7W2Z.ZFw7AzNuza4NAKU08O9hH%2FDV%2FUCpA8N2Va94Zl8v1Uw"
  },
  "body": {},
  "query": {},
  "params": {}
}
{
  "res": {
    "statusCode": 404
  },
  "req": {
    "url": "/assets/profile/avatar.png",
    "headers": {
      "host": "localhost:3020",
      "connection": "close",
      "cache-control": "max-age=0",
      "upgrade-insecure-requests": "1",
      "user-agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.79 Safari/537.36",
      "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
      "accept-encoding": "gzip, deflate, br",
      "accept-language": "en-US,en;q=0.9",
      "cookie": "connect.sid=s%3APwaFQ8_7wVBQDuNVh64lf0KWD0hA7W2Z.ZFw7AzNuza4NAKU08O9hH%2FDV%2FUCpA8N2Va94Zl8v1Uw"
    },
    "method": "GET",
    "httpVersion": "1.0",
    "originalUrl": "/assets/profile/avatar.png",
    "query": {}
  },
  "responseTime": 1,
  "level": "info",
  "message": "HTTP GET /assets/profile/avatar.png"
}

Acceptance criterias

  • [x] Example (log, usecase, etc...)
  • [x] Ts.ED version (bug/question)

Thanks, Romakita :)

question

All 7 comments

Ok found. Just replace this config ['assets']: path.resolve(__dirname, '../assets'),
by '/assets': path.resolve(__dirname, '../assets'),

It will works :)
Romain

Sorry, Romakita. It's my fault. I'm already configuring it with the slash / and it's also clear from the submitted log

[2018-07-04T16:12:55.404] [INFO ] [TSED] - settings.serveStatic => { '/assets': '/home/mouneer/ts-server/assets' }

So what do u think could be the reason for the 404?

Currently the configuration works with the latest version. I'll tried with the version 3, to reproduce it.

Ok have you correctly imported import "ts-express-decorators/servestatic" in your app.ts ?
Because your serve static configuration works with v3 :)

Great! Importing import 'ts-express-decorators/servestatic'; fixes the problem.

Thanks, Romain :)

@Romakita This issue still exists in version : 5.31.1

@dlinx
Can create a new issue please and send your config and more details. Please. This issue is obsolete.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Ionaru picture Ionaru  Β·  5Comments

royibernthal picture royibernthal  Β·  4Comments

yangukmo picture yangukmo  Β·  5Comments

Romakita picture Romakita  Β·  5Comments

RickStanley picture RickStanley  Β·  6Comments