小编Zak*_*Zak的帖子

在 Nodejs Express 应用程序中单击按钮启动/停止 cronjob

我一直在开发一个项目,当用户单击前端的按钮时,需要启动和停止 cron 调度程序。基本上,当用户单击按钮时,cron 作业就会启动。单击停止按钮将停止计时器。它是如此简单。

为了实现这一点,我在按钮单击时向 Nodejs/Express 后端发出发布请求,这会触发调度程序的启动/停止功能。端点如下所示:

const cron = require('node-cron');

router.post('/scheduler', async (req, res) => {

    // gets the id from the button
    const id = req.body.id;

    try{
         // finds the scheduler data from the MongoDB
         const scheduler = await Scheduler.find({ _id: id });

         // checks whether there is a scheduler or not
         if ( !scheduler  ) {
             return res.json({
                  error: 'No scheduler found.'
             });
         }

         // creates the cronjob instance with startScheduler 
         const task = cron.schedule('*/10 * * …
Run Code Online (Sandbox Code Playgroud)

javascript cron scheduler node.js express

6
推荐指数
1
解决办法
5169
查看次数

无法加载资源:服务器在 Nodejs 应用程序中响应状态为 404(未找到)问题

我需要帮助。我在这里查找了解决方案,但仍然找不到。所以我在Goorm ide中练习一些东西,但我已经走进了死胡同。我试图通过将脚本放在页脚中来链接 main.js(位于 js 文件夹中)文件。但我收到一个错误,上面写着

Failed to load resource: the server responded with a status of 404 (Not Found) 我还在 lib 文件夹中上传了一些 png 文件,在尝试访问 ejs 模板中的这些文件后,我遇到了相同的错误。

根文件夹由js、lib 和views 文件夹以及其他一些不相关的文件夹组成。

为了链接 footer.ejs 中的 main.js(位于视图内的partials 文件夹中),我使用了 <script type="text/javascript" src="../../js/main.js"></script>. 上传到 lib/images 文件夹中的 png 图像,我尝试从视图中的 ejs 模板访问这些图像,所以我使用了 <img src="../lib/images/image1.png">. 我在这两种情况下都遇到了这个错误。如果有人可以提供帮助,我们将不胜感激。这就是我的根文件夹的样子 - 在此输入图像描述

编辑: 这是 app.js 代码:

require('dotenv').config();
var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var indexRoute = require("./routes/index");
var flash = require("connect-flash-plus");
var session = require('express-session');

// …
Run Code Online (Sandbox Code Playgroud)

html node.js

5
推荐指数
1
解决办法
1万
查看次数

无法用另一个对象数组更新一个对象数组

我有两个不同的对象数组,storedArray存储在我的文件系统中,并由inputArray用户输入的 update 组成storedArray。每个数组的最小长度为 1,最大数量没有上限。而且它们的长度不一定必须相同。所以我想要的是循环每个数组并且:

  1. 如果来自inputArray匹配的名称与storedArray名称和年龄相同,则在 中不执行任何操作,storedArray但将该对象保留在 中storedArray。(例如约翰)。
  2. 如果 name from与 nameinputArray匹配storedArray且年龄不同,则仅更新 中旧对象中的年龄值storedArray。(例如简)。
  3. 如果内部有一个新对象,inputArray其名称与任何名称都不匹配storedArray,则将新对象推送到storedArray. (例如莫里斯)。
  4. 必须删除与storedArray不匹配的其他对象inputArray。(例如乔安娜、吉姆)。

更新这个

const storedArray = [
        {"name": "John", "age": 25, "courses": 5},
        {"name": "Jane", "age": 21, "courses": 3},
        {"name": "Joanna", "age": 19, "courses": 2},
        {"name": "Jim", "age": 20, "courses": 4},
];
Run Code Online (Sandbox Code Playgroud)

由此

const inputArray = [ …
Run Code Online (Sandbox Code Playgroud)

javascript

4
推荐指数
2
解决办法
6121
查看次数

暗模式在重新加载时闪烁白色背景一毫秒

我正在尝试在我的应用程序中添加此暗模式功能。它使用 localstorage 来存储用户的偏好以供将来使用。所以现在的问题是当启用了暗模式,并且由于某种原因重新加载页面时,同样是用户故意重新加载页面或提交表单,然后页面上到处都是白色背景闪烁,然后变成黑暗的。它停留在几分之一秒。只是看起来不专业。

还没有找到任何解决办法。所以请帮帮我。

附注。下面的代码片段在 SO 中不起作用,因为代码包含localStorage对象。

这是代码:

const toggleSwitch = document.querySelector('#dark-mode-button input[type="checkbox"]');
const currentTheme = localStorage.getItem('theme');

if (currentTheme) {
    document.documentElement.setAttribute('data-theme', currentTheme);
    if (currentTheme === 'dark') {
            toggleSwitch.checked = true;
    }
}

function switchTheme(e) {
    if (e.target.checked) {
        document.documentElement.setAttribute('data-theme', 'dark');
        localStorage.setItem('theme', 'dark');
    }else {        
        document.documentElement.setAttribute('data-theme', 'light');
        localStorage.setItem('theme', 'light');
    }    
}

toggleSwitch.addEventListener('change', switchTheme, false);  
Run Code Online (Sandbox Code Playgroud)
:root {
  --primary-color: #495057;
  --bg-color-primary: #F5F5F5;
}

body{
  background-color: var(--bg-color-primary); 
}

[data-theme="dark"] {
  --primary-color: #8899A6;
  --bg-color-primary: #15202B;
}

table {
  font-family: arial, sans-serif;
  border-collapse: collapse; …
Run Code Online (Sandbox Code Playgroud)

html javascript css

4
推荐指数
1
解决办法
1054
查看次数

element.classList.toggle() 不起作用

我一直试图让我的表单在单击按钮时显示和隐藏,但它不起作用。我想使用普通的 javascript,并一直试图让它工作,.classList.toggle()但它正在工作。

超文本标记语言

<i id="search-button"class="fa fa-laptop fa-2x"></i>
<form id="search-form">
     <input
       id="search-input"
       type="text" 
       name="search" 
       placeholder="Search.." > 
</form>
Run Code Online (Sandbox Code Playgroud)

CSS

#search-input{
  display: none;
}
.show{
  display: block;
}
Run Code Online (Sandbox Code Playgroud)

JS

const searchButton = document.querySelector("#search-button");
const searchInput = document.querySelector("#search-input");

searchButton.addEventListener("click", () => {
  searchInput.classList.toggle("show");
})
Run Code Online (Sandbox Code Playgroud)

<i id="search-button"class="fa fa-laptop fa-2x"></i>
<form id="search-form">
     <input
       id="search-input"
       type="text" 
       name="search" 
       placeholder="Search.." > 
</form>
Run Code Online (Sandbox Code Playgroud)
#search-input{
  display: none;
}
.show{
  display: block;
}
Run Code Online (Sandbox Code Playgroud)
const searchButton = document.querySelector("#search-button");
const searchInput = document.querySelector("#search-input");

searchButton.addEventListener("click", () => {
  searchInput.classList.toggle("show");
})
Run Code Online (Sandbox Code Playgroud)

javascript css

3
推荐指数
1
解决办法
7430
查看次数

仅当第一个输入字段不为空时才在第二个输入字段中添加验证器

我正在使用 Angular Forms 开发一个表单,但我遇到了一个死胡同,我想仅当第一个输入字段不为空时才向第二个输入字段添加验证。

假设我有两个输入字段:姓名和年龄。因此,当在姓名中输入某些内容时,只有年龄才会被设置为必填。我在表单中使用 FormGroup 和 FormControl,这就是组件文件现在的样子,没有年龄验证器:

class Component implements OnChanges {
  @Input() name: string;
  @Input() age: string; 

  form = new FormGroup({
    name: new FormControl(''),
    age: new FormControl('')
  });

  ngOnChanges(changes) {

    if (changes.name?.currentValue !== changes.name?.previousValue) {
      this.setName(changes.name.currentValue);
    }

    if (changes.age?.currentValue !== changes.age?.previousValue) {
      this.setAge(changes.age.currentValue);
    }
  }

  setName(name) {

    this.form.patchValue({
      name,
    });

    if (name) {
      this.form.get('age').setValidators([
         Validators.required,
         this.form.get('age').validator
      ]);
    } 
    
  }

  setAge(age) {
    this.form.patchValue({
      age,
    });
  }

}
Run Code Online (Sandbox Code Playgroud)

这是模板:

<custom-input
      label="Name"
      placeholder="name"
      name="name"
      formControlName="name"
></custom-input>

<custom-input
      label="Age"
      placeholder="age"
      name="age"
      formControlName="age" …
Run Code Online (Sandbox Code Playgroud)

validation angular angular-reactive-forms

3
推荐指数
1
解决办法
3437
查看次数