首页 > 编程语言 >nodejs

nodejs

时间:2024-09-30 11:22:56浏览次数:12  
标签:run nodejs await only test message true

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');
});

标签:run,nodejs,await,only,test,message,true
From: https://www.cnblogs.com/yinchaows/p/18214458

相关文章

  • nodejs学习
    nodejs中的v8引擎模块一、promise钩子模块;二、垃圾回收模块;1、promise钩子包括:(1)onInit(callback)在promise创建时调用;(2)onSettled(callback)在fulfilled或rejected时调用;(3)onBefore(callback)在promise继续执行之前调用的回调;(4)onAfter(callback)在promise继续执行之后调......
  • 基于nodejs+vue心里咨询与诊断平台系统[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着社会节奏的加快与生活压力的增大,心理健康问题日益凸显,成为影响公众生活质量的重要因素。传统心理咨询服务受限于地域、时间以及资源分配不均等问题,难以......
  • 基于nodejs+vue鞋类秒杀商城[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展和电子商务的日益普及,线上购物已成为现代人不可或缺的生活方式之一。在鞋类消费领域,消费者对于时尚、品质与性价比的追求日益增强,......
  • 基于nodejs+vue携手助学助学交流平台[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景在当今社会,教育资源的不均衡分配问题日益凸显,尤其是在偏远地区及经济欠发达地区,优质教育资源的匮乏成为了制约学生成长与发展的关键因素。随着互联网技术的......
  • 基于nodejs+vue协同过滤音乐网站[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着数字音乐产业的蓬勃发展,音乐网站已成为人们日常生活中不可或缺的一部分。然而,面对海量的音乐资源,如何高效、精准地为用户推荐符合其个人喜好的音乐成为......
  • 通过构建具有依赖关系的后端框架来学习 Nodejs
    我在github上为每个尝试涉足后端开发世界(不仅仅是Node.js)的人创建了一本开源(免费)书籍您还可以在本书的网站上以更易于理解的方式访问内容-CacheLane-LearnNode.jstheHardWay这将需要很长时间来构建完成版本(几个月),但不用担心,我已经承诺并承诺每天都会添加新内容。因此,即......
  • 通过示例在 Unity 和 NodeJS 上的游戏中创建安全、快速的多人游戏
    介绍规划多人游戏开发方法-在整个项目的进一步开发中发挥着最重要的作用之一,因为它包含了我们在创建真正高质量的产品时应该考虑的许多标准。在今天的宣言教程中,我们将看一个方法示例,该方法使我们能够创建真正快速的游戏,同时尊重所有安全和反违规规则。所以,让我们定义我们的主要......
  • 解决 Nodejs 中的“Punycode Module is Deprecated”问题
    大家好,我叫asimkhan,目前是metamelon的全栈开发人员。最近,我在为naseebi.com(一个婚姻移动和web应用程序)项目工作时遇到了一个令人沮丧的问题。该问题涉及node.js中punycode模块的弃用,我想与您分享我的经验和解决方案。问题在应用程序中使用配置文件创建功能时,我......
  • 文件系统:Nodejs `fs` 模块
    node.js中的fs(文件系统)模块是一个用于处理文件系统的强大工具,允许您与服务器上的文件和目录进行交互。它内置于node.js中,因此您无需安装任何额外的东西即可使用它。让我们来探讨一下fs的工作原理及其关键功能。1.什么是fs模块?fs模块提供了一个api,用于以紧密围绕标准......
  • 掌握 Nodejs 中的电子邮件发送:分步指南
    发送电子邮件是许多web应用程序中的常见功能,无论是用于用户注册、密码重置还是营销活动。在本指南中,我们将向您展示如何在nodemailer模块的帮助下使用node.js发送电子邮件。我们将涵盖从设置项目到发送html电子邮件和处理附件的所有内容。1.开始使用您的node.js电......