I am so new to node.js.
I am trying to export node.js code that generates file, and use it on script.js (which isn't js) by importing.
Can someone help?
index.js
// import fs module in which writeFile function is defined.
const fsLibrary = require('fs');
// Data which will need to add in a file.
let data = "hfsdafaweaff";
function execute() {
// Write data in 'newfile.txt' .
fsLibrary.writeFile('newfile.txt', data, (error) => {
// In case of a error throw err exception.
if (error) throw err;
});
}
var http = require('http');
var fs = require('fs');
const PORT=8080;
fs.readFile('./index.html', function (err, html) {
if (err) throw err;
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(PORT);
});
module.export = execute;
script.js
import {execute} from './index.js'
document.getElementById("button").onclick = function() {
console.log('hi');
execute();
}
I couldn't find how to export node.js code to ordinary js.