代码读取文本txt文件
1, golang
package main import ( "fmt" "io/ioutil" "net/http" ) func main() { http.HandleFunc("/", serveHome) http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static")))) fmt.Println("Server starting...") http.ListenAndServe(":8080", nil) } func serveHome(w http.ResponseWriter, r *http.Request) { content, err := ioutil.ReadFile("static/update_time.txt") if err != nil { http.Error(w, "File not found.", http.StatusNotFound) return } w.Header().Set("Content-Type", "text/plain") w.Write(content) }
taskkill /F /IM version.exe /T
2, node.js
1, 安装node 2, install open@8.4.2 npm install open@8.4.2 -save 3, run node js node .\version.js node code
const http = require('http'); const hostname = '127.0.0.1'; const port = 3000; let fs = require('fs') console.log(fs); fs.readFile('updatetime.txt', 'utf8', function (err, dataStr) { console.log(err); console.log('-------------'); console.log(dataStr); const server = http.createServer((req, res) => { res.statusCode = 200; res.setHeader('Content-Type', 'text/plain; charset=utf-8'); res.end(dataStr); }); const open = require('open'); const url = 'http://127.0.0.1:3000/' open(url) server.listen(port, hostname, () => { console.log(`Server running at http://${hostname}:${port}/`); }); })