📜 ⬆️ ⬇️

Building our full stack on javascript: The Basics

Building our full stack on javascript: The Basics



In the JavaScript world, it is very easy to type your technology stack using a set of small packages, each of which solves its own specific problem. And this is good, on the one hand, and on the other hand, you have no particular choice - frameworks that perform a wide range of tasks in JavaScript are not popular.


In this series of articles, I want to share my practical experience of building a JS stack.




I switched to full-stack JavaScript from .NET. And after I had mastered the basics of Node, I had to spend a lot more time building my basic stack of technologies and how to organize the code. Unfortunately, there are not so many examples of final applications in JS, where you can see how to solve the main tasks for most projects, such as error handling, database access, code organization, authorization, and so on.


In order to always have at hand an example of using basic technologies, Contoso Express was written, an example of a full-stack application on JS. In our team, we use this project as a starting template for other projects, so it will be maintained and updated in the future.


Github: Contoso Express


Contoso Express is a remake of a famous Microsoft textbook for learning the basics of developing .NET web projects.


Original Contoso Univerisity


For many parts of the stack, it is difficult to stay with one alternative. Therefore, for some technologies there are alternative implementations. See ALT threads in Contoso Express repository.


These articles are for those who already know the basics, if you need to fill in knowledge in a certain area, look for the necessary additional resources .


Let's get started!


Why javascript


The reasons can be many, here is my top list:



And this is not a complete list.


JS language options


Modern JavaScript can be written in several ways:



JavaScript Standards



You can look at the current status of ES6 support at
Kangax ES6 .


Thus, in the near future, it is impossible to develop the client part right away on ES6, because there is no support in all browsers yet.


For the Node, the V8 engine is used, the current stable LTS version of which (4.x) does not support all the new features of ES6.


LTS (long term support) is the NodeJs version recommended for production use. The next LTS Node is expected in October 2016 and already has support for most of the features of ES6.


Transpiling


In order to use the features of ES6 / ES7, there are several transpilers that convert code written on ES6 to ES5.


Pay attention to the difference between transpilation and compilation: here .


Babel is the most popular transpiler from ES6 / Next to ES5.


TypeScript


TypeScript is a JavaScript extension language that adds static typing. Types in TypeScript are used to check the correctness of the code and additional features of refactoring and auto-hints. When TypeScript is transported to JS, all type definitions are omitted.


TypeScript supports many ES6 / ESNext features and can be used as a transpiler (instead of Babel).


In addition, there are additional constructs in TypeScript that are not found in JS - enums, inheritance and polymorphism in classes, and so on. They are transported to JS using auxiliary JS code.


Not all the features of TypeScript are equally useful; I’ll list the main categories:



What to choose


I would recommend choosing between ES6 and TypeScript. ES6 has a lot of useful add-ons that make development easier and more enjoyable and it is worth it to spend more time on the initial setup. On my projects, I switched to TypeScript, because it really seriously improves the development process, although it requires much more effort to configure and integrate. Whatever you choose, it’s good if you make an informed choice by working with both.


Development environment


WebStorm is a smart development environment with many built-in functions. Good support for typeScript. Paid. Configuration details for contoso express on project wiki . WebStorm is the main IDE in our team.


Visual Studio Code is a free environment from Microsoft. The best support for TypeScript, appeared recently, there are some familiar features. It develops quickly, is fully made and supports JavaScript plugins.


Others - you can use other JavaScript IDEs, such as Atom, Sublime, Brackets. TypeScript is more or less supported everywhere.


We select npm packages


Working with npm is easy. Just install and just publish your packages. Because of this, there are a lot of packages on npm (currently 330.000+).


Finding the right package is quite difficult. For any request on npmjs.com there are a lot of packages, and it’s not a fact that the main packages will be provided there for solving the necessary task.


Choosing a package is worth paying attention to: the number of downloads (popularity) and how often the package is updated (commits to the githab repository). Having found the right package, you can search for information on the Internet, so you can find other packages that perform similar tasks.


There is a site with an alternative search on npm npms.io , where characteristics such as popularity and regularity of support are immediately calculated.


In future articles I will describe the basic options for packages for basic development tasks. If the package name is in quotes (for example, "bluebird"), it means that the name with which the package is registered in npm and can be viewed at https://www.npmjs.com/package/{package_name } (i.e. the case of https://www.npmjs.com/package/bluebird ).


Database


SQL or NoSQL?


This question can not be answered unequivocally, depending on the specific situation. The most popular options for SQL / NoSql are PostgreSQL / MongoDb. The recent addition of JSON fields in PostgreSQL, makes it a very attractive option, combining the best of the worlds of SQL / NoSql. But despite this, MongoDb is still the most popular database for Node, and it may be easier to work, especially for those who have not had previous experience with SQL databases.


Database access


Working with a database you can use access directly with the help of a database driver or some kind of higher level ORM abstraction. If you do not have much interaction with the database, it is better to use access directly or a low-level abstraction, such as Knex (for SQL databases).


ORM


Sequelize is the most popular ORM for SQL databases. It provides a high level of abstraction over the database schema and supports basic SQL dialects (PostgreSQL, MySQL, SQLite and MSSQL). Used in Contoso Express.


Knex is a lower level abstraction. More like a query designer than a full-fledged ORM. It supports more dialects and gives more control over the generated SQL. There are functions for constructing a database schema and its migrations.


Bookshelf is another popular ORM based on Knex, the level of abstraction is lower than in Sequelize and many things need to be determined manually.


Mongoose - The most popular ORM for the MongoDB database most popular in JS


Direct connection


For all major databases, there are drivers of good quality, for direct connection. For Postgres, use "pg" or "pg-promise" packages.


What's next


In the next article I will talk about working with JS on the server. It will be more practical and will cover such things as choosing a web framework, organizing the project structure, working with configs, authorization, logging, error handling, and more.


I would be happy to comment comments, especially directly on the project code Contoso Express :)


Thanks to everyone who read to the end! Stay tuned!


')

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


All Articles