小编Kun*_*war的帖子

如何使用 Node JS 访问 DOM 元素?

JavaScript 文件

const path = require('path');
const http = require('http');
const fs = require('fs');

const dir = '../frontend/';

const server = http.createServer((request, respond) => {
      console.log(request.url);

      respond.writeHead(200, {
            'Content-Type': 'text/html',
      });

      const readStream = fs.createReadStream(dir + 'index.html', 'utf-8');
      readStream.pipe(respond);
});

const icons = 'icons/';

fs.readdir(icons, (error, files) => {
      if (error) throw error;

      files.forEach((file) => {
            if (path.extname(file) == '.svg') {
                  // I want to append this filename in the DOM Element.
                  document.querySelector('.container').innerHTML = file; // Like This
                  // console.log(file); …
Run Code Online (Sandbox Code Playgroud)

html javascript node.js

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

标签 统计

html ×1

javascript ×1

node.js ×1