Nodejs Study Notes (iii)

Source: Internet
Author: User
Tags redis desktop manager netbeans

Recently in use sails framework write mobile backstage, immediately on the new year, intends to summarize.

 1. ResourcesNode official website: www.nodejs.orgSails official website: www.sailsjs.orgFind resources: www.github.com, which has most of the NPM packages, can choose the resource bundle you need2. Development tools
  • webstorm:http://www.jetbrains.com/, the company is famous for its Java development tools, and Android Studio has its Java IDE as the underlying support. It's very good code hints, but it's a charge, and it costs money to buy.
  • NetBeans: www.netbeans.org, this IDE I use more, it is free, but it does not provide support for node itself, it just supports the JS syntax, although there is a Nodejs plugin, but not much use, just execute the command in the IDE, and it has a very bad phenomenon, it will not stop scanning external changes , and sails creates temporary files at run time in the. tmp directory under the root directory, and I don't know if that's the reason NetBeans will suddenly slow down when it writes the NODEJS program, so I don't really like using this tool anymore. Of course, NetBeans is pretty good at writing Java programs and PHP programs, like the Laravel framework I use, and it's easy to debug in NetBeans.
  • Aptanata Studio 3:http://www.aptana.com/, this tool is open scripting language, such as PHP, Python, Ruby, but also support JS syntax, but I do not how to use, its PHP debugging function is very powerful.
  • nodeclipse:http://www.nodeclipse.org/, this is an eclipse-based plugin, to use it is not easy, I used a bit on windows, I would like to prompt me to write some of the functions, but directly did not respond, Later on, I don't know why it's so famous.
  • ntvs:https://nodejstools.codeplex.com/, which is a Visual Studio plug-in, is also good, can code hints, support the free Visual Studio Community Edition, and the paid version, However, the Community edition does not support version management and requires the use of version management tools outside the IDE. It also has some undesirable places, such as the format of the code is very strange, some code can be formatted, some cannot, but overall, better, to the next stage with this tool.
  • Sublime:http://www.sublimetext.com/,sublime is a very powerful editor, you can use some plug-ins to code hints, of course, do not prompt themselves to write functions, but for the general application is enough, Because when I was using NetBeans, I couldn't hint my own function or debug it. And it is relatively lightweight and relatively resource-saving. But it needs a fee, although not cracked can be used, but it is not very comfortable.
  • nodepad++:http://notepad-plus-plus.org/, this is a free editor, not as strong as sublime, but also through a number of plug-ins to solve the problem, such as a plug-in Jstool, can be used to format the JS code, relatively lightweight , I often use this to modify minor problems.
In general, I prefer to use NetBeans, Ntvs and nodepad++.3, the handling of common problems:1) session: In order to consider the future extensibility, it is best to save the session, save in Redis, Memcache or MongoDB, the above-mentioned way sails supported by default, I chose Redis,redis can only be installed on Linux, First download the stable version from Redis.io, and then according to the contents of the Readme file, compile, install and install as a service, the process is as follows:in the source code directory, proceed sequentially$ Make$ make Install$ cd Utils$./install_server.shThe commands for viewing redis information on the command line are REDIS-CLI, which can be referenced in official documents or viewed with GUI tools, such as Redis Desktop Manager, which is http://redisdesktop.com/. in the config directory of sails, modify the Session.js file, the content of the modification is similar to the following
Adapter: ' Redis ', host: ' 192.168.1.110 ', port:6379, ttl:86400, db:0,< /c5> Pass: ', prefix: ' This can be used, the use of the remote session in Nodejs, there is a need for the V8 engine memory limit, so do not store unnecessary temporary variables in the main thread , because if the memory limit is reached, it can cause garbage collection or system problems.

2) File Upload

sails file Upload is very simple, but it should be noted that the file upload function needs to be at the end of the form, the following cannot have form input, the basic content is as follows
req.file (' Avatar '). Upload ({ dirname: ' PathName ' }, function (err, uploadedfiles) { if (err) { sails.log.error (err); return res.negotiate (err); } return Res.json ({ message:uploadedFiles.length + ' file (s) uploaded successfully! '  }); });
Avatar is the file name of the upload, if it is a form upload, is <input type=file name=avatar> uploaded files, this file can support multiple files simultaneously upload, if not specify DirName, will be uploaded to the default temporary directory , the file name is randomly generated, uploadfiles is the array, the basic format is as follows [{ FD: ' D:\\temp\\2aee7706-d0a9-4e1e-9ac0-c17ecf48be44.png ', size:48177, type: ' Image/png ', filename: ' aa.png ', status: ' Bufferingorwriting ', field: ' Image ', extra:undefined  } ] the saved file name is UPLOADFILES[I].FD, if you want to modify the filename or move the file, you need to use the standard FS package, you can also use the Fs-extra package, it can be installed NPM, you can also find the appropriate information on github.com. in the mobile background development, often to upload files and save to the database, that is, upload files to temporary files, while saving information, and then manipulate the file, so the client needs to upload the file name or address, call the FS package to move or rename the file.
3) ORMthe ORM used by sails is waterline, it can be used, relatively simple, the general content is as follows:
    • Database connection is saved in Config/connection.js, you can select a variety of databases, but note the need to install the appropriate driver
    • The default model configuration is saved in Config/model.js, including some default settings, such as the default database connection (connection), the default properties (attributes), the default porting method (migrate), Migration mode refers to whether the database is generated according to the model, I am accustomed to the database, and then write the model, so this is set to safe, this file can also save some of the table fields, such as Createdat,updatedat
    • Configuration file model: saved in the Api/model directory, the basic content is as follows
      Module.exports = {
tableName: ' S_user ',
attributes: {
User_code: {
type: ' String '
  }, 
User_password: {
type: ' String '
  }, 
User_email: {
type: ' String '
  }, 
user_mobile: {
type: ' String '
  } 
 } 
};
Overall, very simple, TableName is the name of the table in the database, attributes is the column name, by default the property value is the column name, if not the same, you can add a ColumnName property, if you associate another table, add the Model property, as follows
module.exports = {
tableName: ' T_DEP ',
attributes: {
hospital_name: {type: ' String '},
hospital_grade: {type: ' Integer '},
province: {model: ' District '},
City
: {model: ' District '},
County: {model: ' District '},
hospital_address: {type: ' String '},
hospital_desc: {type: ' String '},
hospital_longititude: {type: ' float '},
hospital_latitude: {type: ' float '},
hospital_postcode: {type: ' String '},
Hospital_tel: {type: ' String '},
Created_user: {model: ' user '},
Updated_user: {model: ' User '}
    }
};
    • Call
The
call is very simple, in the controller file under Api/controller, directly using the module name, do not need require, such as user.create (), User.find () and other basic methods are as follows: . Count () . Create () . Destroy () . Find () . FindOne () . Findorcreate () . Native () . Query () . Stream () . Update () These methods are all very easy to see and see the official documentation for SAILSJS.
    • Populate
populate the data used to get the associated table, such as Doctor.find ({id:doctor_id}). Populate (' user '). EXEC (function (err, doctors) {}),
very easy to handle in some cases
    • Attention:
It is important
to note that these methods are called asynchronously, so it is not possible to query and update a nested callback function when complex logic is required, or to use a Process Control property that does not exist in the attribute content of the async library in the configuration table. So remember to update attribute after updating the database.

4) WebSocket

WebSocket is suitable for information push, the overall relatively simple, in the browser is very easy to deal with, in the Android has not been tested, but in order to develop efficiency, choose the homing pigeon push, and then have the opportunity to study it

Nodejs Study Notes (iii)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.