nodejs

Introduction For Nodejs

base on v8 engine, and signal thread, and asynconous I/O.

  1. 基于v8, 性能出色
  2. 单线程, 没有线程开销,没有多线程相关的复杂的问题(锁,池等,the source of bug.), 隐藏了,其实node 自己本身底层的实现有多线程池, 只是上层不必考虑.
  3. 异步I/O, 不会浪费cpu等待时间,提高并发.

安装

有很多种方式,推荐使用 nvm.

An Example Of NodeJS

const http = require('http');

const hostname = '127.0.0.1';
const port = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

getting start

concurrency model and Event Loop

run-to-completion -- 没有一个直接的认识...(能否举个例子?)

Each message is processed completely before any other message is processed.
This offers some nice properties when reasoning about your program, including the fact that whenever a function runs, it cannot be preempted and will run entirely before any other code runs (and can modify data the function manipulates). This differs from C, for instance, where if a function runs in a thread, it may be stopped at any point by the runtime system to run some other code in another thread.
A downside of this model is that if a message takes too long to complete, the web application is unable to process user interactions like click or scroll. The browser mitigates this with the "a script is taking too long to run" dialog. A good practice to follow is to make message processing short and if possible cut down one message into several messages.

About setTimeout function

The function setTimeout is called with 2 arguments: a message to add to the queue, and a time value (optional; defaults to 0). The time value represents the (minimum) delay after which the message will be pushed into the queue. If there is no other message in the queue, and the stack is empty, the message is processed right after the delay. However, if there are messages, the setTimeout message will have to wait for other messages to be processed. For this reason, the second argument indicates a minimum time — not a guaranteed time.

test module

using test module for testing instead of mocha.

It can also collecting code coverage...

// Sat April 01 10:00	2023
// The standard test module. node:test
import assert from "assert";
import test from "node:test";
import should from "should";

// It's equals to describe
test("It should be empty", () => {
    let me = {}
    me.should.is.empty();
})

test("It should hava a name field", () => {
    let me = {name: "yinchao"}
    me.should.have.property("name", "yinchao");
})

// It's equals to it from mocha.
test("multi level testting...", async (t) => {
    await t.test("multi-level testting 1", () => {
        assert.strictEqual(1, 1);
    });

    await t.test("multi-level testting 2", () => {
        assert.match("I am yinchao, ", /\w/);
    });
})

// skip and only
test("testting skip", {skip: true}, (t, done) => {
    // this code is never executed...
})
test("testting skip2", {skip: "this is skip message"}, (t, done) => {
    // this code is never executed...
})
test("testting skip3", (t, done) => {
    t.skip("some message to be displayed in TAP output");
})
test("testting skip4", (t, done) => {
    t.skip();
})

// If `Node.js` is started with the `--test-only` command-line option,
// it is possible to skip all top level tests except for a selected subset
// by passing the only option to the tests that should be run.
// run with `node --test-only testtest.js`
test("test for `node --test-only`", {only: true}, (t, done) => {
    // await assert.ok(true);

    t.runOnly(true);
    t.test('this subtest is now skipped');
    // await t.test('this subtest is run', { only: true });

    setImmediate(done);
})

test("test for `node --test-only` and for 'passed a callback but also returned a Promise'", {only: true}, async (t) => {
    // await assert.ok(true);

    t.runOnly(true);
    await t.test('this subtest is now skipped');
    await t.test('this subtest is run', { only: true });
})

// Assume Node.js is run with the --test-only command-line option.
// The 'only' option is set, so this test is run.
test('this test is run', { only: true }, async (t) => {
    // Within this test, all subtests are run by default.
    await t.test('running subtest');

    // The test context can be updated to run subtests with the 'only' option.
    t.runOnly(true);
    await t.test('this subtest is now skipped');
    await t.test('this subtest is run', { only: true });

    // Switch the context back to execute all tests.
    t.runOnly(false);
    await t.test('this subtest is now run');

    // Explicitly do not run these tests.
    await t.test('skipped subtest 3', { only: false });
    await t.test('skipped subtest 4', { skip: true });
    });

    // The 'only' option is not set, so this test is skipped.
    test('this test is not run', () => {
    // This code is not run.
    throw new Error('fail');
});
posted @ 2024-09-30 11:26  yinchao_ws  阅读(3)  评论(0编辑  收藏  举报