Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the operating system based on JavaScript?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces you how the JavaScript-based operating system is, the content is very detailed, interested friends can refer to, hope to be helpful to you.

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 for all kinds of low-level tasks, such as hardware communications, but other than that, it uses Node.js. The development of NodeOS began two years ago, and the creator's purpose was simple. He just wondered, "is it possible to create an operating system using only Node.js?"

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

How about this idea?

We've seen that Node.js has grown so rapidly in recent years, so why don't we make it cooler? Like using it as an operating system.

User-independent 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 actually the root of their own file system (root), they can install the package to the system without any super permissions or configure anything, because the package is installed in their own home directory by default. In addition, this also provides good security, if the hacker broke into an account, then only access to the part of the user, the end result is that the hacker can not affect the entire system.

Node.js and NPM

If you think about it, if an operating system uses Node.js, it means that all packages available in NPM are also NodeOS packages. At the time of this writing, there are more than 210000 software packages, and they are growing every minute. It wouldn't be surprising if NodeOS had 1 million apps in a few years' time.

Based on Linux kernel

This doesn't seem like a big deal. Linux is the operating system used by most servers. Because NodeOS is based on the Linux kernel, you can use applications developed for other Linux distributions with little modification.

Bad news

I very much hope that NodeOS is a finished work, but it is not yet. It still lacks some of the key features necessary for the server operating system. For example, the complete BASH toolset, including ps, tail, nano, and grep, is missing. To make matters worse, you can't use it as a desktop operating system because it doesn't have GUI. Of course, you can implement some missing features with just a little bit of JavaScript, but by default, none of these features are available yet, which is too bad.

Well, can I try NodeOS?

Use Docker to experience

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

A computer running Mac OSX or Linux, or maybe a Windows, but I haven't tried.

Docker .

When you have installed Docker, it is easy to run an instance of NodeOS. You only need to execute the following command, and Docker will do this magical thing 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 an image of NodeOS from the repository and installs it into a virtual environment. Once installed, a SSH session connected to NodeOS will be opened.

What about not using docker?

In some cases, you may not be able to experience it with Docker, or you may want to try the * version of NodeOS. At the time of this writing, the NodeOS image was generated two months ago, while the development version was updated six days ago. So, if you want to use the * version, you should start with the source code. It's not that hard, but it takes some time. You need to:

A computer running Linux. You can compile it on OS X, but cross-platform compilation takes a lot of time, as well as for Windows.

Linux compiles and builds related tools (make, gcc +, gcc, autoconf).

Qemu .

It really takes a lot of time.

If everything is ready, you can start compiling from the source code:

Download the project source code: bash git clone git@github.com:NodeOS/NodeOS.git.

Compile with the following command: cd NodeOS and npm install.

I quoted the official document word for word: "take the popcorn and see a movie, no kidding, really." Oh, yes, it takes a lot of time. Do something interesting.

Execute bash npm start to run NodeOS in Qemu.

Are you ready to work?

When the installation is complete, we can see if it works by executing the ls command in NodeOS's shell. The output looks like this:

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

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

Command not found: ifconfig

There seems to be no ifconfig command. This is because NodeOS does not have the ifconfig command by default. What do we do now? 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 with the following command:

Npkg install bin-ifconfig

If all goes well, the ifconfig command can now be used in shell. Let's try to execute 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 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 works. You have successfully installed your NodeOS application: ifconfig.

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

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

Use 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 simple and can do nothing but be used as a text editor, but it's enough for us.

It's easy to create a file with Hipster, just run hip filename, such as hip package.json. To save the file, press Ctrl + s and exit and press Ctrl + Q.

Here, we used the code developed by the main developer of NodeOS, and I didn't really develop the 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 * NodeOS application. Like every Node.js application (or NPM package), we start by creating a package.json file with the following contents:

{"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-evident. This bin collection 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 collection contains a list of NPM packages required by the application. In our example, the author of the code includes the Blessed package, a curses-like library that allows Node.js to support API with an advanced terminal interface.

Now we get to the main part, the actual code.

#! / usr/bin/env node

This part is called shebang. NodeOS doesn't really need it, but it's used to tell the operating system how to execute the following code. What this means here is that it tells the system that every line of code below needs to be interpreted and executed by the / 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 no command is specified to view. The same is true in our code example: if no second parameter is given (the * * parameter is man itself), the default value for 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 there is a readme file for the given application. In NodeOS, the installation path for each application is lib/node_modules under its home directory (/). If the README.md file exists, save its contents to the readme variable. Otherwise, display an error message and exit.

/ / 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, and it's easy to display the contents of a file by creating a box and then loading the contents.

Screen.key (['escape',' QQ, 'Cmurc'], function (ch, key) {return process.exit (0);})

Now, let's find a way to exit the man application. We combined escape, Q or emacs style Cmurc 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 (['baked ch, key) {box.scroll (- box.height); screen.render ();})

We use arrow keys to scroll up and down, space, f, j, or n to page down, b, k, or p to page up.

Box.focus (); screen.render ()

We asked the application to focus the input on box, where we created and rendered everything.

Store the file edited above in the / lib/node_modules/bin-man directory (named 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 almost completed our * NodeOS custom applications. * there is one small step left. We need to create a configuration file required by the NodeOS application. Quite simply, create it to / etc/bin-man/config.json and the content is just an empty JSON object: {}.

Now we can try our new application. Run man in NodeOS, which will show the readme file we created earlier.

As you can see, it's easy to implement anything in NodeOS. You only need to know Node.js.

NodeOS has a lot of potential, and I think it will become a great operating system when more functions are implemented. There's still a lot of work to do, but with the prosperity of the entire Node.js ecosystem, it's not surprising if it becomes a popular operating system one day.

About how the JavaScript-based operating system is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report