Sunday, 14 May 2017

Error in serving a specific file using express.static

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");



via Shiven Sinha

No comments:

Post a Comment