📜 ⬆️ ⬇️

Hello World on Derby.js



If you are not indifferent to new tendencies of web development, then I invite you to participate in the discussions in the comments to the post Angular vs Meteor vs Derby . There are many interesting thoughts.

But in the meantime, the week of Derby.js on Habré continues. The population of Derby programmers is doubled. And today we will learn to run on ostriches to set up the environment, create an application, launch and consider its structure.
If for you this is a completed stage, you may be interested to watch the Tutorial, which is essentially Faq . The rest is welcome under cat.


Environment


We assume that you have a Debian Linux family (Ubuntu, Debian, etc.). Setting up the environment for other operating systems: other Linux, Windows, Mac OS has its own characteristics, but it is not fundamentally different.
In order to run the Derby application, we need: node.js, mongodb, redis (minimum 2.6).
')
For node.js and redis, we will use the chris-lea repository, mongo has an official one.

#   # node.js sudo add-apt-repository -y ppa:chris-lea/node.js # mongodb sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv 7F0CEB10 sudo echo 'deb http://downloads-distro.mongodb.org/repo/ubuntu-upstart dist 10gen' | sudo tee /etc/apt/sources.list.d/10gen.list # redis sudo add-apt-repository -y ppa:chris-lea/redis-server #  apt-get sudo apt-get -y update #  sudo apt-get -y install nodejs sudo apt-get -y install mongodb-10gen sudo apt-get -y install redis-server 


application


Of course we can now file down the application from scratch. What should we build a house?
But Derby has a utility that generates an application layout for us and saves time. Why don't we take advantage of this?
To do this, we need to install the npm package derby globally:

 sudo npm install -g derby 


Create an application called hello-derby (this will also be the folder name):

 derby new hello-derby 


The utility will create an application for us and install all dependencies. It will take some time and at the end you will see:

  Project created! Try it out: $ cd hello-derby $ npm start More info at: http://derbyjs.com/ 


We will consider a js application, but if you want Coffeescript, use --coffee, -c:

 derby new --coffee my-cool-coffee-derby-app 


You can also create an empty application (layout only):

 derby bare my-bare-app 


Or create an application, but do not install dependencies with --noinstall, -n:

 derby new -n empty-node_modules-app 


Run


To run, you guessed it, you need:

 cd hello-derby npm start 


We see:

 1234 listening. Go to: http://localhost:3000/ 


Now in the browser we go here: http: // localhost: 3000 /
Hurray, it works!

Structure


Let's quickly run through the project structure.

/ lib - almost all js here. If you are on coffee, then there will be a / src folder.
/ lib / app is a client application called app. There may be several such applications. In general, this is something that can be executed on the server and also downloaded to the client.
/lib/app/index.js - this is where the application itself is created, the components 'derby-ui-boot' (this is bootstap for derby) and 'ui' (these are custom components) are added. Then routes are created that will be executed both on the server and on the client. At the end, controllers are created - these are functions that are executed only on the client and are associated with dom's manipulations.
/ lib / server is a server application. There can only be one. Here the code that runs only on the server and is not directly accessible from the client.
/lib/server/error.js - here we generate custom static error pages (without the client application).
/lib/server/index.js - an express.js application is created here, databases are configured, a store is created, add connect modules to the express app, some of which are parts of the Derby application. At the end, a server-side route is created, which gives an error if no route has worked before.
/ node_modules - here npm packages.
/ styles - here are the styles. Default Stylys. May be different for different client applications (by folder names). In ui, component styles.
/ ui - here are the components. Each component consists of js and html files and is located in a folder with its own name.
/ ui / connectionAlert is an example component. If the client went offline, this component displays the corresponding caption and the “Reconnect” button. If the re-connect fails, he suggests restarting the Reload application.
/ui/index.js - here the general settings of components.
/ views - html templates.
/ views / app - here those templates that will be loaded into the client application app.
/views/app/home.html is the start page.
/views/app/index.html is the layout for home.html and list.html.
/views/app/list.html - list.
/ views / error - here are templates for errors that we download from /lib/server/error.js
.npmignore - this you will need if you publish your application to npm as a package.
Procfile is for Heroku
README.md - read me
package.json is the settings for npm. All your dependencies are listed here. And also what to do if you scored npm start.
server.js is the most important file. The entry point of your application. This is where Derby launches the express application.

Derby.js materials

Source: https://habr.com/ru/post/195864/


All Articles