JavaScript based operating system have you heard of it? _javascript Tips

Source: Internet
Author: User
Tags virtual environment docker run

I think most people have heard of Node.js, but have you ever heard of Nodeos? Yes, Nodeos, an operating system written in Node.js. Well, to be honest, Nodeos uses the Linux kernel to handle a variety of low-level tasks, such as hardware communications, but in addition, it's all about node.js. Nodeos's development began two years ago, and the creator's goal was simple, and he was just curious, "is it possible to create an operating system using only node.js?" ”

Is it possible to create an operating system using only node.js?

What about the idea?

We have seen that Node.js has made such rapid progress in recent years, so why don't we make it cooler? Like using it as an operating system.

User-Independent File system

Nodeos introduces an interesting feature: All users have a separate file system, and they do all kinds of work in a simple file system. Because their "home directory" is in fact the root of their own file system, they can install packages into the system without any super privileges, and they don't need to configure anything, because the packages are installed in their own home directory by default. In addition, this also provides good security, if the hacker into an account, then only access to the user's part, the end result is that the hacker does not affect the entire system.

Node.js and NPM

You might think that if an operating system uses node.js, it means that all the packages available in NPM are also nodeos packages. When writing this article, there are already more than 210,000 packages, and every second is growing. If a few years later, Nodeos has 1 million applications, that's no surprise.

Based on Linux kernel

This does not seem to be anything, Linux is the operating system used by most servers. Because Nodeos is based on the Linux kernel, you need only a few modifications to use the apps that were developed for other Linux distributions.

Bad news.

I very much hope that Nodeos is a finished work, but it is not yet. It still lacks some of the critical functionality required by the server's operating system. For example, missing the full set of BASH tools, including PS, tail, nano, and grep. What's worse, you can't use it as a desktop operating system because it doesn't have a GUI. Of course, you can implement some of the missing features, just a little bit of JavaScript, but by default, these features are not, it's bad.

Well, can I try Nodeos?

Use Docker to experience
The easiest and quickest way to experience Nodeos is as follows:

1, a running MAC OS X or Linux computer, maybe Windows can also, but I did not try.
2, Docker.
After you install the Docker, it is easy to run a Nodeos instance. You just have to execute the following commands, and Docker will do the Magic for you:

sudo docker run-t-i Nodeos/nodeos


The easiest and quickest way to experience Nodeos is through Docker.

When you run the above command, Docker automatically downloads the Nodeos image from the warehouse and installs it into a virtual environment. Once installed, a SSH session that is connected to Nodeos is opened.

No Docker?
In some cases you may not be able to use Docker to experience, or you want to experience the latest version of Nodeos. When writing this article, Nodeos's mirrors were generated two months ago, and the development version was updated six days ago. So, if you want to use the latest version, you should start with the source code. It's not too hard, but it takes some time. You need to:

1, a computer running Linux. You can compile it on OS X, but it takes a lot of time to compile across platforms, as well as Windows.
2, Linux compile and build related tools (make, g++, GCC, autoconf).
3, Qemu.
4, time, really need a lot.
If you have everything you want, you can start by compiling the source code:

1, compile with the following command: CD Nodeos and NPM install.
2, I quoted the official document verbatim: "Take the popcorn to see a movie, no kidding, really." "Yes, it takes a lot of time to do something interesting."
3. Execute bash NPM start to run Nodeos in Qemu.
can we work now?
When the installation is complete, we can see if it works by executing the LS command in the Nodeos shell. The output is similar to the following:

[' etc ', ' lib ', ' lib64 ', ' root ', ' bin ', ' sys ', ' usr ', ' share ', ' proc ']

If shown as above, some basic commands are available to work. But what if we want to know the network card address? Under Linux, this command is ifconfig, let's try:

Command not Found:ifconfig

There seems to be no ifconfig command. This is because Nodeos does not have the Ifconfig command by default. Now what? Quite simply, Nodeos has an integrated package manager (like apt or yum), called Npkg, which is based on Node's NPM and is easy to use. You can easily install Ifconfig by using the following commands:

Npkg Install Bin-ifconfig

If everything works, the Ifconfig command can now be used in the shell. Let's try to do it again, and the output looks like this: (I replaced the MAC address):

eth0:flags=8863 MTU 1500
 ether 01:23:45:67:89:ab 
 inet6 f0cd::ef01:0203:0405:181%en1 Prefixlen-ScopeID C4/>inet 192.168.0.21 netmask 0xffffff00 broadcast 192.168.0.21
 -Nd6
 options=1
 Status: Active

If your output is similar to the above, it means that it can work. You have successfully installed your first Nodeos application: Ifconfig.

It can work, but what can we do with this operating system?

What is the value of this if we can only use the Node.js-written operating system to do what you do (or less) on Ubuntu or other Linux distributions? In fact, the most interesting part of the whole thing is that everything is node.js developed. This means that we can develop our applications just by using Node.js. For example, the Nodeos does not have the default implementation of the Man command, which is used to display Help information for other commands. Don't worry, it's easy to make it happen.

Using Node.js to build a Nodeos application

First, let's install a text editor called Hipster so that we can create and edit files. Execute the following command: npm install-g hipster@0.15.0. This text editor is so simple that nothing can be done except with a composition editor, but it's enough for us.

Creating files with hipster is simple, running hip filename, such as hip Package.json. To save the file, press CTRL + S to exit and press CTRL + Q.

Here, we use the code developed by a Nodeos master developer, and I'm not really going to develop this application myself. The original code in our example can be found in the Node-bin-man Git repository.

Let's go back and create our first Nodeos application. Like every node.js application (or NPM package), we start by creating a Package.json file that reads as follows:

{
 "name": "Bin-man", "
 Version": "0.0.1",
 "description": "Format and display manual pages",
 "bin": {
 "Man": "Man.js"
 },
 "repository": "Https://github.com/groundwater/node-bin-man",
 "author": " Groundwater ",
 " license ":" MIT ",
 " dependencies ": {
 " blessed ":" ~0.0.22 "
 }
}

These parameters name, version, author, repository, license, and description are self-explanatory. This bin collection is a key-value pair object of JSON that contains the command name and its associated JavaScript file. In our example, the Man command is associated with a man.js file. The Dependencies collection contains a list of the NPM packages needed for this application. In our example, the author of the Code contains the blessed package, a class-curses library that allows Node.js to support the API for advanced terminal interfaces.

Now we are entering the main part of the actual code.

#!/usr/bin/env node

This part is called the Shebang (the "companion"). Nodeos does not actually need it, but it is used to tell the operating system how to execute the following code. In this sense, it tells the system that every line of code below needs to be interpreted by/usr/bin/env node command.

var fs = require (' FS ');
var blessed = require (' blessed ');

As in Node.js, the Require () function loads the selected package into memory and saves it as a specific variable.

var arg = process.argv[2] | | ' Bin-man ';

The standard behavior of the man command is to display its own Help information if you do not specify a command to view. The same is true in our code example: if the second argument is not given (the first argument is the man itself), then the default value for the parameter is Bin-man.

var path = Process.env.HOME + "/lib/node_modules/" + arg + "/readme.md";

try{
 var readme = fs.readfilesync (path, ' utf-8 ');
} catch (e) {
 console.log (' No readme.md for Package ', ARG);
 Process.exit ( -1);
}

Here, the program checks whether a given application has a readme file. In Nodeos, the installation path for each application is lib/node_modules under its home directory (/). If the readme.md file exists, its contents are saved to the README variable. Otherwise, an error message is displayed and exits.

Create a Screen object.
var screen = Blessed.screen ();

var box = Blessed.box ({
 content:readme,
 alwaysscroll:true,
 scrollable:true,
});

Append we box to the screen.
Screen.append (box);

Blessed has a very simple API to display the contents of a file is easy, just create a box, and then load the content.

Screen.key ([' Escape ', ' Q ', ' c-c '], function (CH, key) {return
 process.exit (0);
});

Now, let's find a way to exit the man application. We have a combination of escape, q, or Emacs style c-c to exit the application.

Screen.key ([' Space ', ' F ', ' j ', ' n '], function (CH, key) {
 box.scroll (box.height);
 Screen.render ();
});

Screen.key ([' Down '], function (CH, key) {
 box.scroll (1);
 Screen.render ();
});

Screen.key ([' Up '], function (CH, key) {
 box.scroll ( -1);
 Screen.render ();
});

Screen.key ([' B ', ' K ', ' p '], function (CH, key) {
 box.scroll (-box.height);
 Screen.render ();
});

We use the arrow keys to roll up and down, page down with space, F, J or N, and B, K, or P to page up.

Box.focus ();
Screen.render ();

Finally, we let the application put the input focus into box, where we create and render all the content.

Store the edited file in the/lib/node_modules/bin-man directory (name is man.js) and add a simple readme.md, similar to the following:

# man

Author: @groundwater

# # Install

npkg Install bin-man

# Usage

'
Usage:man Pkgname

Display A packages readme.md file
""

We have basically finished our first Nodeos custom application. Finally, one small step, we need to create a Nodeos application needs of the configuration file. Very simply, to create it to/etc/bin-man/config.json, the content is just an empty JSON object: {}.

Now we can try our new application. Running Man in Nodeos, it will show the Readme file we created earlier.

Summarize

As you can see, it's easy to achieve anything in Nodeos, you just have to know node.js.

Nodeos has great potential and I think it will become a great operating system when more features are implemented. There is still a lot of work to be done, but it's no surprise that in the event of a thriving node.js ecosystem, it will soon become a popular operating 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.