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

查看:84
本文介绍了如何使用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

我已经从网站上获得了保存在assss文件夹中的图像.现在,我需要以该脚本需要创建的新动态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?

推荐答案

您可以使用文件系统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天全站免登陆