There are at least two sane choices:
Serve other folders by using app.UseStaticFiles
. The original solution is from Ode to Code. I use it for
development, because Visual Studio doesn't seem to respect local
.npmrc
file set up with prefix = wwwroot/node_modules
. Ideally,
node_modules
should be bundled for production. There is npm rollup
plugin that can automatically bundle scripts using
import
feature (ES2015).
Serve node_modules from CDN (e.g. unpkg.com). This is fairly simple, the only downside is CDN's
response time, especially if you've disabled browser caching for
development purposes.
Here is the code to serve folders in ASP.NET Core. You only need to change the Startup
class:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
...some other stuff
if (env.IsDevelopment())
{
ServeFromDirectory(app, env, "node_modules");
}
}
public void ServeFromDirectory(IApplicationBuilder app, IHostingEnvironment env, string path)
{
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(
Path.Combine(env.ContentRootPath, path)
),
RequestPath = "/" + path
});
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…