Kano001/nodejs8
0
1const express = require('express');2const bodyparser = require('body-parser');3const txt2image = require("./txt2images")4const fs = require('fs'); //引入fs模块5 6// Constants7const PORT = 7860;8const HOST = '0.0.0.0';9 10// App11const app = express();12app.use(bodyparser.urlencoded({ extended: true }))13app.post("/txt2pic", async (req, res) => {14 var prompt = req.body.prompt;15 var size = req.body.size;16 var url = await txt2image.txt2img(prompt, size)17 res.send(url)18})19app.get('/', (req, res) => {20 res.writeHead(200, { 'Content-Type': 'text/html' })21 var html = fs.readFileSync(__dirname + '/static/index.html', 'utf-8'); //读取html文件,__dirname表示当前文件所在的目录路径22 res.end(html);23});24app.get('/jquery', (req, res) => {25 res.writeHead(200, { "Content-Type": "application/javascript" })26 var js = fs.readFileSync(__dirname + '/static/jquery.js', 'utf-8'); //读取jquery文件,__dirname表示当前文件所在的目录路径27 res.end(js);28});29 30app.listen(PORT, HOST, () => {31 if (HOST == '0.0.0.0') {32 console.log(`Running on http://127.0.0.1:${PORT}`);33 } else {34 console.log(`Running on http://${HOST}:${PORT}`);35 }36 37});