Stylesheet not loaded because of MIME-type

前端 未结 30 2529
猫巷女王i
猫巷女王i 2020-11-22 07:29

I\'m working on a website that uses gulp to compile and browser sync to keep the browser synchronised with my changes.

The gulp task compiles everything

30条回答
  •  难免孤独
    2020-11-22 08:14

    This is specific to Typescript+Express

    I ctrl+f'd "Typescript" and ".ts" and found nothing in these answers, so I'll add my solution here, since it was caused by (my inexperience with) typescript, and the solutions I've read don't explicit solve this particular issue.

    The problem was that Typescript was compiling my app.ts file into a javascript file in my project's dist directory, dist/app.js

    Here's my directory structure, see if you can spot the problem:

    ├── app.ts
    ├── dist
    │   ├── app.js
    │   ├── app.js.map
    │   └── js
    │       ├── dbclient.js
    │       ├── dbclient.js.map
    │       ├── mutators.js
    │       └── mutators.js.map
    ├── public
    │   ├── css
    │   │   └── styles.css
    ├── tsconfig.json
    ├── tslint.json
    └── views
        ├── index.hbs
        └── results.hbs
    

    My problem is that in app.ts, I was telling express to set my public directory as /public, which would be a valid path if Node actually were running Typescript. But Node is running the compiled javascript, app.js, which is in the dist directory.

    So having app.ts pretend it's dist/app.js solved my problem. Thus, I fixed the problem in app.ts by changing

    app.use(e.static(path.join(__dirname, "/public")));
    

    to

    app.use(e.static(path.join(__dirname, "../public")));
    

提交回复
热议问题