Chinese Documentation : Creating an application

Prerequisites

Icon

Related articles:

See also:


Creating a new application

As you saw in Getting Started > Create a simple API, the easiest way to create an application is to use slc loopback, the application generator.  

Icon

It is possible to create a LoopBack application by coding it from scratch, but slc loopback does all the "heavy lifting" to create the basic scaffolding of the standard project layout. You can then customize the application to suit your needs.  When you create your application this way, you can continue to use slc loopback to add models, data sources, and so on.

In general, the documentation assumes you've created your application using slc loopback.

Once you create your application, you may want to configure it, for example: Turn off stack traces, disable API Explorer, and retrieve the values of environment variables.  See Environment-specific configuration for more information.

Standard project layout

The application generator creates an application with the standard project layout.  To summarize:

  • server directory
    • server.js - Main application script; see below.
    • config.json - Global application settings, such as the REST API root, host name and port to use, and so on. See config.json.
    • model-config.json - Binds models to data sources and specifies whether a model is exposed over REST, among other things.  See model-config.json.
    • datasources.json - Data source configuration file. See datasources.json.
  • client directory (empty except for a README stub)
  • common/models directory - created when you create a model with the Model generatorslc loopback:model.
    • A JSON file and a JavaScript file for each model (for example, my-model.json and my-model.js).

Main application script (server.js)

This is the main application script in the standard scaffolded application, as created by slc loopback.


1 - 3
: Require LoopBack modules and set up standard objects loopback, app, and boot.

 

6: Initialize (boot) the application.

7+: Start the application and web server.

var loopback = require('loopback');
var boot = require('loopback-boot');
var app = module.exports = loopback();
// Bootstrap the application, configure models, datasources and middleware.
// Sub-apps like REST API are mounted via boot scripts.
boot(app, __dirname);
app.start = function() {
  // start the web server
  return app.listen(function() {
    app.emit('started');
    console.log('Web server listening at: %s', app.get('url'));
  });
};
// start the server if `$ node server.js`
if (require.main === module) {
  app.start();
}

 

 

Attachments:

api explorer.png (image/png)