Have you heard of JavaScript-based operating systems ?, Javascript

Source: Internet
Author: User
Tags virtual environment docker run

Have you heard of JavaScript-based operating systems ?, Javascript

I think most people have heard of Node. js, but have you heard of NodeOS? That's right, NodeOS, an operating system written in Node. js. Well, to be honest, NodeOS uses the Linux kernel to handle various underlying tasks, such as hardware communication, but it uses Node. js. The development of NodeOS started two years ago. The goal of the creator was very simple. He was just curious: "Is it possible to create only one operating system using Node. js ?"

Is it possible to create only one operating system using Node. js?

How is this idea?

We have seen the rapid development of Node. js over the past few years. Why don't we make it cooler? For example, you can use it as an operating system.

Independent File System

NodeOS introduces an interesting feature: all users have an independent file system that completes various work in a simple file system. Because their "main directory" is actually the root of their own file system, they can install the software package to the system without any super permissions, you do not need to configure anything because the software package is installed in their own home directory by default. In addition, this provides good security. If a hacker attacks an account, he can only access the part of the user. The final result is that the hacker cannot influence the entire system.

Node. js and NPM

You can think about it. If an operating system uses Node. js, it means that all packages available in NPM are also NodeOS software packages. At the time of writing this article, we already have more than 0.21 million software packages, and every minute is growing. If NodeOS has 1 million applications a few years later, that's nothing strange.

Linux Kernel-based

Linux is the operating system used by most servers. Because NodeOS is based on the Linux kernel, you only need to make few changes to use the applications developed for other Linux distributions.

Bad message

I very much hope that NodeOS is a finished file, but it is not yet. It still lacks some key functions necessary for the server operating system. For example, a complete BASH tool set, including ps, tail, nano, and grep, is missing. Even worse, you cannot use it as a desktop operating system because it does not have a GUI. Of course, you can implement some missing functions. You only need to use a little JavaScript, but by default, these functions are not yet available yet, which is really bad.

Okay. Can I try NodeOS?

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

1. A computer running Mac OSX or Linux may work in Windows, but I have never tried it.
2. Docker.
After installing Docker, it is easy to run a NodeOS instance. You only need to execute the following command, and Docker will do everything for you:

sudo docker run -t -i nodeos/nodeos


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

After you run the preceding command, Docker automatically downloads the NodeOS image from the repository and installs it in a virtual environment. After the installation, an SSH session connected to NodeOS will be opened.

What about docker?
In some cases, you may not be able to use Docker or try the latest NodeOS version. At the time of writing this article, the NodeOS image was generated two months ago, and the Development version was updated six days ago. Therefore, if you want to use the latest version, you should start with the source code. This is not difficult, but it takes some time. You need:

1. A computer running Linux. You can compile it on OS X, but cross-platform compilation takes a lot of time, as does Windows.
2. tools related to Linux compilation and building (make, g ++, gcc, and autoconf ).
3. Qemu.
4. A lot of time is required.
If everything is ready, you can start compiling from the source code:

1. Use the following command to compile: cd NodeOS and npm install.
2. I quoted its official document word by word: "Take popcorn and watch a movie. It's no joke, really .", Yes, it takes a lot of time to do something interesting.
3. Execute bash npm start to run NodeOS in Qemu.
Can I work?
After the installation is complete, we can execute the ls command in the shell of NodeOS to check whether it works. The output is similar to the following:

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

If the preceding figure is displayed, some basic commands can work. But what if we want to know the NIC address? In Linux, this command is ifconfig. Let's try:

command not found: ifconfig

It seems that there is no ifconfig command. This is because NodeOS does not have the ifconfig command by default. What should I do now? NodeOS has an integrated package manager (similar to apt or yum) called npkg. It is Node-based NPM and is easy to use. Run the following command to install ifconfig conveniently:

npkg install bin-ifconfig

If everything is normal, the ifconfig command can be used in shell now. Let's try again, and the output is similar to the following: (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 64 scopeid 0x5  inet 192.168.0.21 netmask 0xffffff00 broadcast 192.168.0.21 nd6 options=1 media: autoselect status: active

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

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

If we can only use the operating system written in Node. js to achieve the same (or fewer) things on Ubuntu or other Linux distributions, what value does it have? In fact, the most interesting thing in the whole thing is that everything is developed by Node. js. This means that we can use Node. js to develop our applications. For example, there is no default man command in NodeOS, which is used to display help information of other commands. Don't worry, it's easy to implement.

Build a NodeOS application using Node. js

First, let's install a text editor called Hipster so that we can create and edit files. Run the following command:Npm install-g hipster@0.15.0. This text editor is very simple and can't do anything except for text editing, but it is enough for us.

Creating a file with Hipster is simple. Run hip filename, for example, 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 primary developer. I did not 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 from creating a package. json file with the following content:

{ "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 include name, version, author, repository, license, and description. This bin set is a JSON key-Value Pair object that contains the command name and its associated JavaScript file. In our example, the man command is associated with the man. js file. The dependencies set contains the list of NPM packages required by the application. In our example, the author of the Code contains the Blessed package, which is a library of curses and allows Node. js to support APIs on the advanced Terminal interface.

Now we have entered the main part, the actual code.

#!/usr/bin/env node

This part is called shebang ). NodeOS does not actually need it, but it is used to tell the operating system how to execute the following code. It indicates that each line of code in the system must be interpreted and executed using the/usr/bin/env node command.

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

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

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

The standard behavior of a man command is to display its own help information if the command to be viewed is not specified. The same is also true in our code example: if the second parameter is not provided (the first parameter is man itself), the default value of this 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 of each application is lib/node_modules in the main directory. If the README. md file exists, save the content 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 our box to the screen.screen.append(box);

Blessed has a very simple API. to display the content of a file, you only need to create a box and load the content.

screen.key(['escape', 'q', 'C-c'], function(ch, key) { return process.exit(0);});

Now, let's find a method to exit the man application. We combined C-c in the escape, q, or emacs style 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. We use space, f, j, or n to flip down pages, and B, k, or p to flip up pages.

box.focus();screen.render();

Finally, let the app focus on the input in box. Here we create and render all the content.

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

# ManAuthor: @groundwater## Installnpkg install bin-man## Usage```Usage: man PKGNAMEDisplay a packages README.md file```

We have basically completed our first NodeOS custom application. The last small step is to create a configuration file for the NodeOS application. It is very easy to create it in/etc/bin-man/config. json. The content is just an empty JSON object :{}.

Now we can try our new application. Run man in NodeOS to display the readme file we created earlier.

Summary

As you can see, implementing the current Node. js in NodeOS is very simple. You only need to understand Node. js.

NodeOS has great potential. I think it will become a great operating system when more features are implemented. At present, there is still a lot of work to be done, but with the flourishing development of the entire Node. js ecosystem, it is no surprise that it will soon become a popular operating system.

Articles you may be interested in:
  • Crazy, there is a js-written operating system
  • JavaScript checks the scripts of browsers and Operating Systems
  • Jsp obtains the browser and operating system information of the client.
  • Use JavaScript to obtain the operating system version of the client.
  • Obtain the function code of the client operating system under js (1: vista, 2: windows7, 4: xp)
  • JavaScript advanced programming Reading Notes (16) javascript detection browser and operating system-detect. js
  • Use Javascript to determine the type of the operating system for compatibility under different operating systems
  • Example of JS obtaining the browser version and operating system version
  • Encapsulated js judgment operating system and browser code sharing

Related Article

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.