Nodejs execution shell of online compiling system

Source: Internet
Author: User

The company recently made a simple gadget that is a simple on-line compiling system. Now do a few simple summary records.

1, the problem of the path.

The path that the node program executes may affect relative path usage in the program. (This need to be further studied, analysis theory) Therefore, the path module is used. I didn't see how important this module was before, and it's convenient to find it this time. Here are some of the ones I used.

The following is just your own simple understanding, may not be expressed so accurate, please refer to Nodejs's official website API. (The English version is recommended, the Chinese version is slightly incomplete)

Path.resolve (from, to): The From is an absolute path, to is a relative path, the absolute path to the final to. Note that to a directory, this path gets the path to a directory, but the path does not have a "/" at the end. If the file is the absolute path to the file.

Path.relative (from,to): The From and to are absolute paths, and get the relative path from the from relative to.

Path.basename (P,[ext]): Gets the last file name of the path, including the suffix name. If the EXT parameter entry is added, only the name of the file is returned, excluding the suffix name.

Path.extname (P): Gets the file suffix name, including ".".

Path.dirname (P): Gets the directory name of the path. Does not include the last "/".

Path.join (ARGS1, arg2 ...): Merges individual parameters into one path and normalizes them.

2. Nodejs Execution Shell

In this compilation system, a lot of things are done with the shell. The shell is really important, and it takes some time to add theoretical knowledge.

Execute the shell in Nodejs, using child_process. There are three ways to Spawn,exec and execfile. Spawn not how to use, so I have no say, the next exec and execfile. The former is recommended to use a small statement, of course, you can execute multiple statements, each command string with a delimiter connection. It is recommended that you use a file if you have more than one statement.

A very important point in executing the shell is the path problem. Because the relative path used in the shell encounters a failure. Because executing a shell, a new child process is created, and the proces.env of the main process is passed to the child county. Need to enter the child process in the main process money using Process.chmdir (), switch to the shell's execution directory. Nodejs API is also very thoughtful, which has a opts parameter, you can set the OPTS.CWD. To change the execution environment of the shell.

3. Simple Shell Programming

I have no shell programming experience, so I have no authority to say anything. One thing is the transfer of parameters, $ $ is the first parameter, and so on.

4. Async

One of the async modules is very important. Because many asynchronous functions are designed in code, the code structure is very messy. The use of async can be a more concise and easy-to-read program.

Async.series ({one:func1 (CB) {}, TWO.FUNC2 (CB) {} ... function (err,) {});}) ;

Returns whenever a function has an error. Eventually, each CB is composed of an object element. You can also set an array.

More needs further study.

5. About programming

At the beginning of the task of their own work, the code is very messy. And the pain caused by the change in the middle of demand ..... Later it was slightly refactored with object-oriented refactoring. Therefore, the future as far as possible to establish object-oriented programming thinking, more stereoscopic, but also more conducive to maintenance.

Nodejs execution shell of online compiling system

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.