0

We have a server application that uses Web API 2 to expose restful API. To help clients use our API better, we developed a js library that wraps API calls. Instead of distributing the js library to the clients, we'd like them to reference them directly from the server.

To make it easy on clients, I'd like this line to work:

<script src="serverUri/scriptsFolder/clientLibrary.js">

Is that possible? (using Web API 2, we'd prefer to have as fewer dependencies as possible)

1
  • as per your Question, CDN is the prefered solution as answered by @Andrei Commented Jun 7, 2016 at 10:38

1 Answer 1

2

This isn't something an Api should concern itself with. Basically I would choose another delivery mechanism.

The way I see it you have two options :

  1. put it on a CDN somewhere and provide your client with the link to it - which you would have to do anyway.
  2. Distribute it as an npm package or nuget package and let them reference it that way.

You could expose it from the api itself but there are a number of issues with that approach. You basically give your clients a choice with the other approaches. If you change the version of the file you could potentially break all your clients very quickly. Controlling your delivery mechanism gives them an option to upgrade when it's convenient to them.

To be fair I did add a scripts folder to a web api2 project, put a js file in there and I was able to access it directly with no issue, so it can be done, but I still don't think it's a good idea :)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Andrei. when you create a new ASP MVC project though, it creates a Scripts folder with some default js libraries. then, when you add an HTML page, you can reference those libraries using <script src ="Scripts/library.js">, is this a bad practice? if not, why can't I achieve the same behavior using WebAPI2 ?
create the project in a different way .... empty and just tick WebApi2, then it gievs only what you need for the api itself and none of the MVC folders. As for best practice, as I said I would not put js files there as explained in my main reply. You do what you have to do though :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.