未加星标

Getting Started with Node.js and LoopBack

字体大小 | |
[前端(javascript) 所属分类 前端(javascript) | 发布者 店小二05 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏
Introduction

As APIs become more and more ubiquitous, it becomes increasingly important than ever that your application successfully delivers production quality APIs to your users in a fast and efficient manner. Node.js and LoopBack are here to help you do just that.

LoopBack is a highly-extensible, open-source Node.js framework that enables you to create dynamic end-to-end REST APIs with little or no coding.

By the end of this article, you'll learn how to:

Install the LoopBack framework, Create a custom API endpoint using the slc command-line tool, and Test your custom endpoint with mocha , chai and supertest . Prerequisites

We will be using the latest LTS version of Node.js, which at the time of writing, is v6.6.0.

Node.js - download at nodejs.org or install via Node Version Manager , and npm - v.3.10.3 included with your Node.js installation. Project Setup

We'll get started by installing the strongloop package. This package is essential for working with the LoopBack framework. It includes:

The StrongLoop command-line tool, slc , for creating LoopBack applications and for running and managing Node applications, The LoopBack framework, including loopback, loopback-datasource-juggler modules, and numerous other related StrongLoop modules, along with modules that they require, StrongLoop Arc, the unified graphical tool suite for the API lifecycle, including tools for building, profiling and monitoring Node applications, LoopBack Angular command line tools, and Various other tools, including Yeoman, the LoopBack Yeoman generators to create and scaffold LoopBack applications, and Grunt ― the javascript task runner. npm install -g strongloop

Now, let's use the newly installed slc command-line tool to initialize our LoopBack application.

slc loopback getting-started-node-loopback

We're creating a new LoopBack application called getting-started-node-loopback . You'll be greeted by the ever-friendly Yeoman , who will ask you a few questions about the application.


Getting Started with Node.js and LoopBack

For our example, the defaults are fine. This will create a new directory called getting-started-node-loopback , create a skeleton for our API server, and install the necessary npm packages.

Finally, we'll install a few additional packages that we'll need for testing.

npm install --save-dev mocha chai supertest

Did you know that at this point, we already have a fully functioning API as well as an API explorer? That's the simplicity and power of LoopBack! Let's take a look.

From the application directory run:

node server/server.js

Browse to http://localhost:3000/explorer and you'll see the built-in API explorer based upon the popular Swagger framework and the standard LoopBack user model with no less than 28 predefined methods ― all without writing a single line of code.


Getting Started with Node.js and LoopBack

However, we're not stopping there. It's time to create our own endpoint.

Creating Tests

Wait, we're creating our tests before even writing a single line of code for our endpoint? That's right. That's Test-driven Development.

We're going to create our tests, watch them fail and then create our endpoint so that our tests pass. TDD relies on a very short development lifecycle where code is written only to pass specific test cases. We're not going to talk too much about TDD right now, but you can check out this article about TDD with Node.js writen by Raja Sekar if you would like to learn more about it.

Let's create our test directory and a single file that we'll use for our endpoint tests.

For our endpoint, we're going to create some fictional clients of a business.

mkdir test cd test touch client.test.js

Open the client.test.js file and add this code:

'use strict'; var expect = require('chai').expect; var supertest = require('supertest'); var api = supertest('http://localhost:3000/api'); describe('Client', function() { it('should get all clients', function(done) { api.get('/clients') .expect('Content-Type', /json/) .expect(200) .end(function(err, res) { if (err) { return done(err); } var clients = res.body; expect(clients.length).to.be.above(1); done(); }); }); it('should get a single client', function(done) { api.get('/clients/findOne') .expect('Content-Type', /json/) .expect(200) .end(function(err, res) { if (err) { return done(err); } var client = res.body; expect(Array.isArray(client)).to.be.false; done(); }); }); });

Here, we are adding two mostly standard tests using the popular mocha , chai and supertest modules. One of the tests checks that the clients endpoint returns an array of clients and the second checks for a single client from /clients/findOne . The findOne method in LoopBack is the same as the standard find method, except it is limited to one record.

Of course, as we've said before, these won't pass at the moment since we have yet to create the clients endpoint.

If you run the tests, you'll get something like this:

$ ./node_modules/mocha/bin/mocha test/client.test.js Client 1) should get all clients 2) should get a single client 0 passing (67ms) 2 failing 1) Client should get all clients: Error: expected 200 "OK", got 404 "Not Found" at Test._assertStatus (node_modules/supertest/lib/test.js:250:12) at Test._assertFunction (node_modules/supertest/lib/test.js:265:11) at Test.assert (node_modules/supertest/lib/test.js:153:18) at assert (node_modules/supertest/lib/test.js:131:12) at node_modules/supertest/lib/test.js:128:5 at Test.Request.callback (node_modules/superagent/lib/node/index.js:603:3) at node_modules/superagent/lib/node/index.js:767:18 at Stream.<anonymous> (node_modules/superagent/lib/node/parsers/json.js:16:7) at Unzip.<anonymous> (node_modules/superagent/lib/node/utils.js:108:12) at endReadableNT (_stream_readable.js:921:12) 2) Client should get a single client: Error: expected 200 "OK", got 404 "Not Found" at Test._assertStatus (node_modules/supertest/lib/test.js:250:12) at Test._assertFunction (node_modules/supertest/lib/test.js:265:11) at Test.assert (node_modules/supertest/lib/test.js:153:18) at assert (node_modules/supertest/lib/test.js:131:12) at node_modules/supertest/lib/test.js:128:5 at Test.Request.callback (node_modules/superagent/lib/node/index.js:603:3) at node_modules/superagent/lib/node/index.js:767:18 at Stream.<anonymous> (node_modules/superagent/lib/node/parsers/json.js:16:7) at Unzip.<anonymous> (node_modules/superagent/lib/node/utils.js:108:12) at endReadableNT (_stream_readable.js:921:12)

Exactly what we expected. In TDD, it's sometimes a good thing to see a failed test, but it means we still have some work to do. Now, onto creating our endpoint and making those tests pass.

Creating the Model

We'll again use the StrongLoop CLI, this time to create our model representing our business clients. LoopBack defines a model as something that represents data in backend systems such as databases, and by default has both Node and REST APIs.

As we're more interested in the REST API for this example, our model will be exposed as our /clients endpoint.

$ slc loopback:model ? Enter the model name: client ? Select the data-source to attach client to: db (memory) ? Select model's base class PersistedModel ? Expose client via the REST API? Yes ? Custom plural form (used to build REST URL): ? Common model or server only? common

You'll again be prompted by slc to answer some questions. Enter the model name as client and accept the defaults for the rest. If you are interesting in learning more about the model generator, you can read more about it in the LoopBack documentation .

You may have noticed that we're using the in-memory database for our example. Clearly, this isn't acceptable for production use, but thankfully LoopBack also provides a number of connectors for popular SQL and NoSQL databases, including mysql, PostgreSQL, Microsoft SQL Server, Oracle, and MongoDB among others. If you can't find a connector for your database of choice there, then check GitHub as there are also many community contributed connectors for additional data stores.

After completing the above steps, slc will ask if you want to add some properties to the model. Say yes and add two required properties, one string called name and one boolean called active .

While we're only using a few for our example, there are quite a number of additional interesting options for properties in LoopBack models. You can again consult the LoopBack documentation for further detail.

When you are done, hit enter again to exit the CLI.

Let's add some client properties now. Enter an empty property name when done. ? Property name: name invoke loopback:property ? Property type: string ? Required? Yes ? Default value[leave blank for none]: Let's add another client property. Enter an empty property name when done. ? Property name: active invoke loopback:property ? Property type: boolean ? Required? Yes ? Default value[leave blank for none]: Let's add another client property. Enter an empty property name when done. ? Property name:

If you were to try to run the tests again, you'd notice that they still fail, albeit the first test fails with a different error:

Uncaught AssertionError: expected 0 to be above 1

That's because we haven't added any clients to our client database yet. Let's add a client with curl.

curl -X POST --header "Content-Type: application/json" --header "Accept: application/json" -d " { \"name\":\"Bob\", \"active\":true }" http://localhost:3000/api/clients

LoopBack will send back a response such as:

{"name":"Bob","active":true,"id":1}

Now, let's try running our tests again:

$ ./node_modules/mocha/bin/mocha test/client.test.js Client 1) should get all clients should get a single client 1 passing (40ms) 1 failing 1) Client should get all clients: Uncaught AssertionError: expected 1 to be above 1 at Test.<anonymous>

本文前端(javascript)相关术语:javascript是什么意思 javascript下载 javascript权威指南 javascript基础教程 javascript 正则表达式 javascript设计模式 javascript高级程序设计 精通javascript javascript教程

主题: Node.jsRESTSQLMongoDBGitMySQLJavaScriptGitHubJavaGrunt
分页:12
转载请注明
本文标题:Getting Started with Node.js and LoopBack
本站链接:http://www.codesec.net/view/481999.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 前端(javascript) | 评论(0) | 阅读(31)