0

I am using a node server to serve files and also build an API for an app, which also uses Angular 4. I am facing trouble serving a specific file from the node_modules folder. The file should be at url http://localhost/node_modules/@angular/material/prebuilt-themes/indigo-pink.css. But the address is automatically redirected to the URL with a trailing slash, and gives the response 'Cannot GET /node_modules/@angular/material/prebuilt-themes/indigo-pink.css/'.

Other files in the same directory are being served without any problem. Also, renaming this file cures this problem. Here is the code:

var express = require('express'),
  mongoose = require('mongoose'),
  bodyParser = require('body-parser'),
  routes = require('./server/routes.js'),
  fs = require('fs'),
  path = require('path');

var app = express();

mongoose.connect("mongodb://127.0.0.1");

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use("/", express.static(path.join(__dirname, 'dist')));
app.use("/node_modules", express.static(path.join(__dirname, 'node_modules')));
routes(app); //API
app.listen(80);
console.log("Listening");
Shiven Sinha
  • 656
  • 5
  • 14
  • Have you tried `app.use( express.static(__dirname + '/dist') ); ` ? – eko May 14 '17 at 19:51
  • @echonax The file is actually in /node_modules/, so the statement `app.use("/node_modules", express.static(path.join(__dirname, 'node_modules')));` must have been responsible for that. I tried changing that to `__dirname + '/node_modules'`, but it gives the same error. – Shiven Sinha May 15 '17 at 06:13
  • No I meant the line above that – eko May 15 '17 at 06:38
  • @echonax Yep, I tried changing that too. But it still won't work. – Shiven Sinha May 15 '17 at 06:50
  • 1
    Have you checked http://stackoverflow.com/questions/13442377/redirect-all-trailing-slashes-globally-in-express? – eko May 15 '17 at 06:52
  • @echonax That did work! But what must be causing this? Should I report an issue? – Shiven Sinha May 15 '17 at 07:16
  • I don't know :-) but let's mark this as a dupe. – eko May 15 '17 at 07:19
  • Possible duplicate of [Redirect all trailing slashes globally in express](http://stackoverflow.com/questions/13442377/redirect-all-trailing-slashes-globally-in-express) – eko May 15 '17 at 07:19

0 Answers0