如何使用 javascript 创建 HTML 文件 [英] How to Create HTML FILE with javascript

查看:24
本文介绍了如何使用 javascript 创建 HTML 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 website-scraper npm 来抓取网站,https://github.com/website-scraper/node-website-scraper

I'm using website-scraper npm for scraper an website, https://github.com/website-scraper/node-website-scraper

我已经从保存在assests文件夹中的网站获取了图像.现在我需要在这个脚本需要创建的新动态 html 中显示图像并显示这些图像.我看到了这篇文章:使用 JavaScript 创建 HTML 文件但它似乎没有帮助.

I already got the Images from the website saved in assests folder. now I need to show the Images in a new dynamic html that this script need to create and to show this images. I saw this post: Create HTML file with JavaScript but its not seems to help.

如何仅使用 JavaScript 创建 HTML?创建一个真正的 html 文件并将其保存在本地.还是我应该使用 Nodejs?将图像传递到服务器端并将图像附加到 hbs?其他选择?

How can I create an HTML using only JavaScript? create a real html File and save it locally. or should I use Nodejs? pass the images to server side and to attach the images to a hbs? Other options?

推荐答案

你可以使用 filesystem API 将您想要的任何内容写入任何文件.您可以使用任何您想要的方法(例如字符串连接或使用 Underscore.js 模板)创建一个 HTML 字符串,然后使用 Node.js 文件系统 API 将其写入任何文件.

You could use filesystem API to write anything you want to any file. You would create an HTML string using any method you want (string concatenation for example or using Underscore.js templates) and then write it to any file using the Node.js filesystem API.

var fs = require('fs');

var htmlContent = '<html>Whatever</html>';

fs.writeFile('/my-page.html', htmlContent, (error) => { /* handle error */ });

这篇关于如何使用 javascript 创建 HTML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆