For security reasons, the client-side cannot directly write to the server-side as they're not connected with one another. If writing a file to the server is what you want, you'll need a backend server and some kind of API/script that you interact with and create files that way.
There are other ways that don't involved creating files, such as using a database or cloud solution like Firebase. However, there are perfectly valid reasons you might want to keep your site basic and write static files (speed being one of them) and not wanting to setup and maintain a database being another.
This can be done using the fs
module in Node.js for example:
var fs = require('fs');
function generateHtml (req) {
return '<!DOCTYPE html><html><header><title>My Page</title></header><body><p>A test page</p></body></html>';
}
var filename = '/path/to/secondindex.html';
var stream = fs.createWriteStream(filename);
stream.once('open', function(fd) {
var html = generateHtml();
stream.write(html);
stream.end();
});
The above would be in a Javascript file server-side and then you would have some kind of server checking for a request (preferably this would be protected using a token or something) and creating the needed file.
The fs
module in particular is flexible in that you can create files many different ways. The above uses streams and is great for when you're dealing with potentially massive files.