-
Notifications
You must be signed in to change notification settings - Fork 1
/
exercise11.js
24 lines (22 loc) · 929 Bytes
/
exercise11.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
(function () {
var fileName = process.argv[2] ? process.argv[2] : 'example.html'
var fileContent = '<!DOCTYPE html>\n<html lang="en">\n<head>\n<meta charset="UTF-8">\n<title>Node Exercise 10</title>\n</head>\n<body>\n<h1>Page Title</h1>\n<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Consequuntur, molestias.</p></body></html>'
var fs = require('fs')
fs.writeFile(fileName, fileContent, function (error) {
if (error) throw error
console.log('File saved with name: ' + fileContent)
})
var http = require('http')
var hostname = '127.0.0.1'
var PORT = 8080
var server = http.createServer()
server.listen(PORT, hostname)
server.on('request', function (request, response) {
response.statusCode = 200
response.setHeader('Content-Type', 'text/html')
fs.readFile(fileName, 'utf-8', function (error, data) {
if (error) throw error
response.end(data)
})
})
})()