 So, hi, I'm Erin McKean. I am in that grayed out text that you cannot read a developer evangelist for Loopback. And Loopback is an open source node framework for making APIs, which I will now talk about at length. So the goal of this talk is to go on Jeopardy. So I wanted to prototype an API that would help me learn Jeopardy questions. I would like to beat IBM's Watson. I like to aim high and win money. To do this, I need an API, obviously, because anything worth doing needs an API. I need data, and that's about it. So I think it's easy to build an API, to prototype an API against a dataset with Loopback, Mongo, and BlueMix, which I'll just touch on in this talk, which of course you can deploy to using Cloud Foundry. When I say it's easy, I mean it's easy to make an API. It's not easy to actually win on Jeopardy. So let's talk a little bit about what Loopback is. How many people have used Loopback in this room? Just me then. Oh, somebody else. So how many people in this room consider themselves node developers? Great. And this is not really directly related, but how many people in this room have used BlueMix and or API Connect? All right. So Loopback is a tool for rapidly generating REST APIs with node. It is open source and maintained by IBM. It's based on Express. Have people used Express in this room? Yeah, Express is very well known. It's free to use. It costs you zero to use Loopback, and you can deploy a Loopback application anywhere you can deploy a node application because a Loopback application is a node application. And I think that the number one reason to use Loopback is that it's very, very fast to develop an application with Loopback. I like to call Loopback CakeMix for APIs. So you add your data, and you stir, and then you have APIs. And just as with CakeMix, you can make the standard cake right out of the box. You can also with Loopback make a fancy cake. You can take the basic Loopback app that you get, and you can customize it in all sorts of ways. It doesn't mean that you can't make the cake of your dreams. It just means you can make the cake of your dreams faster. And the reason I like to use the CakeMix metaphor is how many people like to eat cake? I like to eat cake. How many people like to bake cake as much as you like to eat cake? Very few people like to bake as much as they like to eat. So with Loopback, you get to the cake stage faster, with less baking. So this is a real world API that I'll talk about. So here's the data. So I just happened to have 200,000 plus questions from the show Jeopardy in JSON. And the reason I have this data is because there is a wonderful tiny letter that comes out almost weekly called Data is Plural, in which the author of this tiny letter sends links to open data sets that you can download and play with. And it's everything from climate data to there's another database I like to use, which is all of the codes that are on the stickers on fruit and vegetables in the grocery store. That's an open data set that you can play with. So Data is Plural, set me a link to this data set. It was scraped by a dude on Reddit, and it was probably the first time that I've ever been thankful for a dude on Reddit. But so this data has a really nice simple schema, very straightforward, great fun data set to play with. And I've been keeping it in Mongo because I'm a node developer, and that's where node developers tend to keep stuff. So in order to kind of, if you wanted to follow along, which you don't have to do, this is kind of your setup. This is what you need to build this kind of particular flavor of API. Node, NPM, I also really like using NVM. If you haven't used NVM, the node version manager, it lets you switch between different versions of node, you know, on demand. If you used a similar one for Ruby, it's the same thing, but it's great if you want to try out newer versions of node while not blowing away your current node setup. Local Mongo, and then I'll show you how it works on Bluemix as well. Okay, so let's go ahead and do this live. Hopefully, let's go back to, here we go, is this big enough for folks? People in the back? Readable? Awesome. Okay, so let me get out of this. Start with a nice clean console. So I have already installed the loopback CLI. It's just NPM install, loopback dash CLI. If you've ever installed a node module, there is nothing different about it. Install it globally. So now I have access to the loopback app, scaffolding, the CLI for scaffolding, and I'm just going to make myself a nap. And we're going to see our friendly Yeoman generator here, and he's going to very helpfully remind us that what we're doing is creating a loopback application. And let's call this CFJeopardy. We'll put it in that directory. Can people read this all right? Should I make it a little bit bigger or brighter? Let's see if that helps. So I'm going to use the current version of loopback, loopback version three. And there are lots of app options that the scaffolding gives you here. But I'm just going to use an empty server because I'm not going to add off in this example. I don't want to hello world, and I'm not going to build a note-taking application, so let's just use the empty server. And so now the helpful mombats at NPM are going to install all of our dependencies for us, and conference Wi-Fi hasn't been too bad today. I've been pretty impressed. All right, so now I'm going to switch in to that directory. So now I have the app scaffolded, and I'll show you what that code actually isn't really an API at all. And so I mentioned earlier we're going to be using Mongo. Loopback can connect to all sorts of databases. Basically anything that you can think of, loopback can connect to. So wherever you like to keep your data, loopback can make an API from it. So in memory, DB2, big surprise, IBM's DB2 database is supported very well. Redis, Mongo, all flavors of SQL, Postgres, you can actually connect to SOAP services if you have to do that. So I'll just scroll through here. Elasticsearch, and some of these connectors are created and supported by the strong loop team at IBM, and some of them are from the community. The Elasticsearch connector is actually a community connector, and it's really nice. So let's just choose Mongo. Because I am lazy, I use a clipboard manager. How many people here use a clipboard manager? Dudes, a clipboard manager will improve your life so much. It lets you save the last 100, 200 things that you have copied and pasted, so that you can copy and paste them at any point. I like copy and paste, here I'll show you what it looks like, copy and paste. It costs like $5 or something. I like to pay people for software because that means that someday people will pay me for software. It's like the circle of life for software development. So even though it's a paid app, I really recommend it. Honestly, if you take away nothing from this presentation other than I should really use a clipboard manager, I will have changed your life. It saves me like an hour a week. So, rant over, I'm gonna use a connection string here. Because I use a connection string to connect to my local Mongo, I don't have to specify the host, it's already in that string. I don't have to specify the port, ditto. Because I'm running a local Mongo, I haven't put a username or password on it. If you are running Mongo in production and does not have a username or a password, be prepared to send Bitcoin to somebody in the very near future to get your database back. And I don't have to specify the database also because that's also in the connection string. And now I will install the connector. So Loopback doesn't install the connector dependency until you tell it what database you want to connect to. So it won't install like 25 connectors right off the bat. Okay, so now I have an app and now I have a data source. Now I want to tell it, Loopback, what model I want to use for my data. And I bet you could all guess that the command is LBModel. So this is an exercise in how many different ways I can spell Jeopardy. This is the way I like to spell it. So I'm just going to call it JeopardyQuestion. I'm going to connect it to that data source that I just created. I'm going to use a persisted model here, and that's usually the default. Because a persisted model, you can both read and save to the database. And who knows, maybe I want to invent my own JeopardyQuestions or scrape some more. Let's expose it via the REST API because otherwise what is the point? It does not need to plural. I'm going to use the common model here because who knows, someday I might wake up and really want to write a client application for this API. I doubt it, but I might. But putting the model in the common folder means the client has access to it. So what kind of properties does our data model or our schema have for JeopardyQuestions? Well, it's right there in the name. So they have questions, which are strings, and they're required, no default value. They also have answers, which are also strings, also required, no default. And let's add just one more category, one more property of category, which are also strings and required. And now we're done. And when I say now we're done, I mean literally now we are done. We now have a working application. So let's look at it. So this is one I was using earlier that I called CF6. But when I refresh this, you'll see that I gave, it's the directory name that I created for this example. All of these endpoints were created by loopback. So let's just try one. So let's count how many questions I have in this database so you can tell I'm not lying. There are 216,930 questions in the state of this. So let's see one. Let's find one, because I made the example of getAll earlier, and it just chugged forever because it was trying to get 216,000 questions. Here we go, for the last eight years of his life, Galileo was under house arrest for espousing this man's theory, who was Copernicus, that's all you need to do to get a full credit API with loopback. It's that simple. But, let's just take a look at the code real fast. If you wanna see what it actually looks like. I have been using Visual Studio quite a bit lately, but I'll just show this in Sublime. So here is the scaffolding of the app. There's a client folder which is basically like, here's where you put your client. Here's the common folder where we saved that model that we created. And here's the JSON for that model. If you go in later and you decide you wanna add properties or change the name for properties or change whether properties are required or not. You can just type straight into this file. You don't need to go back to the command line. This is all completely editable. If you wanna customize your loopback application, you can add new endpoints here, and I'll show you that in just a minute. Here's our actual app. This is it. That's all it does. Here's where our information about our data source goes. And loopback apps are really easy to make 12 factor compatible apps. So you could have a data sources file that's data sources.development.json for your development credentials. You can have a data sources.production.json file where you keep your production credentials that can refer to say an environment variable. So it's very easy to take this prototype app and actually deploy it with very few changes. Here's your model configuration. So you can see here's where we named the data source that we wanted to connect. Which is then referenced here. So it makes them very straightforward app scaffold. But let's see how we might customize this app. Because what I wanted to do was study Jeopardy questions. But if I use that find one endpoint, because of the way Manga works, it's gonna find me the same one every time. So I'm not gonna get any better. I'm just gonna see Copernicus over and over again. So what I really wanna have is a random endpoint. I wanna get a random question from these 200,000 questions. So what that looks like is this. So in our models folder, we had that JSON file, which is the schema here, and we also had that JS file. And so in that JS file, all you have to do is write functions for your endpoints. So maybe I'll make this a little bit smaller so it fits on one screen. So here you can see that here's our random endpoint. And what we're gonna do is we're gonna connect to our database connector. And then everything from line seven on is just straight up Mongo syntax. We're gonna use collection aggregate with a sample size of one. And what this does is it just takes the collection in aggregate and returns us one question. And that question is random. And Mongo handles all the randomization for you. There's no math.floor, random floor JavaScript nonsense where you're trying to find a random number and then pull something out of the database from that random number. So it's kind of cool that you not only get all this generated code, but you also have access to the features of whatever database you're using underneath. I also wanted to add another endpoint categories. So what this does is it uses Mongo's collection.distinct. And it finds me all the unique values of the category property in all the records in that collection, which is also I think kind of cool. And then here, Loopback uses the open API spec, which people mostly know as Swagger. So all that Explorer that I showed you was basically the Swaggerized Loopback application. And this is how you add your own endpoints to that Explorer. So here's the name, here's what kind of endpoint it is. It's a description, a description of what it returns. And in this file, this is also where you could block those endpoints from showing up in the Explorer. So I have this application running up on Bluemix. So I can show you what those endpoints look like working in the wild. So here's our random question endpoint. So let's get a random question. Let me blow this up a little bit. And I can just keep hitting that button and keep getting random questions. It really is just that easy to create your own endpoints with Loopback. Let's see if I can find the categories one. The categories one is a little bigger. So here are all the categories. And it will scroll for a very, very, very long time. But it really is just that simple to make APIs with Loopback. And it's also pretty fun. So now I can go back to our regularly scheduled presentation. Get right back to the right slide. So I'm always happy when a demo actually works. But there's lots of other ways that you can customize your Loopback application. You can use custom ID values. We had the IDs automatically inserted. So if I had added a question using a put or a post, it would have generated an ID from Mongo and had a special underscore ID property that I didn't have to set. But if you wanted to use, say, another field as the ID customized that way, you can add hooks that happen at any point in the chain. So you can add hooks that happen when a record is saved. You can add a hook that happens once a record is returned. But before it goes to the consumer, you can add boot scripts. So something can happen when your Loopback app is started. You can hide endpoints. There's a lot more stuff that you can do. And of course, once you have your API, you can build a front end if that's what you like to do. Personally, I prefer to build APIs as they're a lot cleaner. Browsers are terrible. I don't know why people want to build for browsers. But so and also when I talk about Loopback, I also want to talk a little bit about why a framework. Because obviously, all of this is just JavaScript. You could write it yourself in JavaScript. You don't have to use a tool like Loopback. But I think that frameworks can be pretty helpful. I think that when you use a framework, the biggest value you get is it makes the implicit assumptions explicit. Because everything that you're thinking about in your head, a framework says, oh, this thing, this is how you do it. And that creates a shared mental model between all the people who are working on the framework. Whether it's just you, or whether it's you and your whole team, or whether it's you and all the people who use Loopback and work on Loopback. Having this shared mental model means that when I talk about a concept and someone else who's also using Loopback talks about it in the same terms, we can be more confident that we're referring to the same thing. Having a framework, especially one as big as Loopback, in terms of number of users, means that there's an ecosystem. When I'm confused about how to do something with Loopback, first I check the docs, which are really actually pretty amazing. And then I go to the Google group, or I go to Stack Overflow. And usually someone else has asked that same question, because quite frankly, I'm not brilliant enough to come up with questions that no one else has ever asked. And it's really helpful. It promotes code reuse, of course, because a lot of this code is written for you, you don't have to write it yourself. And I also really like to learn from other people's mistakes. It's a lot less painful than learning from my own mistakes. So again, I can go to Stack Overflow, see how somebody did something, and have them tell me what they found painful. And then I can carefully walk around that and not have to deal with it. And then, of course, they help you build faster. It's really fast to use Loopback. So sometimes when people talk to me about Loopback, they're like, yeah, it's really cool. But I don't really like to use frameworks because I think real developers build everything themselves by hand. And I feel a little bad when people say real developers don't use frameworks because I think that real developers are ones who build safe and stable and maintainable applications that users want and need. Because your users do not care whether you handcrafted your JavaScript from pure electrons, or whether you used a framework. What they care about is, did they get their random jeopardy question? So there are tons of Loopback resources. I talked about some of them, Loopback Ios where all the docs live. There's Google Group, Stack Overflow Tag. There's stuff on IBM Developer Works. We're also starting to talk about the next version of Loopback. Loopback 3 is going to be around for a long time. But the next version is going to support a lot of ES next features. So ES17 and other cool stuff. Also, there's been some talk about GraphQL for Loopback, which I think is very exciting. With API Connect, you can manage your Loopback APIs once you build them with all sorts of cool enterprisey things. There's lots of API Connect resources, API Connect pricing, which I'm sure you're all very desperate to know about. If you have questions about this, please hit me up. I'm just Emakeen on Twitter. Strongloop's also very active on Twitter. And this whole repo is available to you where that is real quick so that you can see the URL. So yeah, so this example not only includes the custom code, but it also includes a sample manifest. So if you want to use Cloud Foundry, it'll give you an example of how to push it up and full instructions of how to do so. And I'm more than happy to answer any questions you might have about Loopback. And thank you so much for your kind attention. Any questions for Erin? Oh, I also forgot to say that for y'all JavaScript developers, I come equipped with semicolon appreciation society stickers. So if you would like to join the semicolon appreciation society, let's see, I think I have some of the stickers with me, then please come up and get a sticker. I also have Loopback stickers and, oh yeah, Semicolon Appreciation Society. I also have Loopback stickers and API Connect stickers that I will happily give to you. OK, thank you and have a great evening.