运行 在没有 android studio 的情况下在本地使用 flutter web 应用程序
Running flutter web app locally without android studio
我有一个使用 Firebase 的云 Firestore 的 Flutter 应用程序。我已经完成了 Web 构建,并且通过 Android Studio 在 Chrome 上 运行 运行良好。我想与我的客户分享我的 Web 应用程序进度,但不想托管它(因为它尚未完成)。因此,我想找到一种在本地 运行 它的方法,就像使用 Android Studio 一样,但不需要安装 Android Studio(希望不需要安装 flutter要么),这样我就可以将构建文件发送给我的客户,他们可以 运行 在他们的机器上(使用脚本在本地启动网络服务器和 运行 网络应用程序)。
我尝试了包含在 web 构建文件夹中的以下脚本(index.html 所在的位置)
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from httplib import HTTPResponse
from os import curdir,sep
#Create a index.html aside the code
#Run: python server.py
#After run, try http://localhost:8080/
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.path = '/index.html'
try:
sendReply = False
if self.path.endswith(".html"):
mimeType = 'text/html'
sendReply = True
if sendReply == True:
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type', mimeType)
self.end_headers()
self.wfile.write(f.read())
f.close()
return
except IOError:
self.send_error(404,'File not found!')
def run():
print('http server is starting...')
#by default http server port is 80
server_address = ('127.0.0.1', 8080)
httpd = HTTPServer(server_address, RequestHandler)
try:
print 'http server is running...'
httpd.serve_forever()
except KeyboardInterrupt:
httpd.socket.close()
if __name__ == '__main__':
run()
但是当在 Chrome 上打开 http://localhost:8000
时,我得到一个空白页面并且控制台显示错误:
Failed to load resource: net::ERR_EMPTY_RESPONSE main.dart.js:1
Failed to load resource: net::ERR_EMPTY_RESPONSE manifest.json:1
Failed to load resource: net::ERR_EMPTY_RESPONSE :8080/favicon.png:1
我还通过 运行ning ws --spa index.html
尝试了 NPM local-web-server,但只是得到了 ERR_EMPTY_RESPONSE
响应。
这是我 build/web
在 运行 宁 flutter build web
:
之后的内容
如何创建一个本地服务器,我可以在其中托管我的网络应用程序并在本地 运行 它而不是在 Internet 上托管它?
正如你在评论中提到的那样。
使用以下内容创建文件 app.js
:
const express = require('express')
const app = express()
const port = 8000
app.get('/', (req, res) => {
console.log('getting request')
res.sendFile('website/y.html',{root:__dirname})
})
app.use(express.static(__dirname + '/website'))
app.use((req, res)=>{
res.redirect('/')
})
app.listen(port, () => {
console.log(`app listening at http://localhost:${port}`)
})
我的网站文件位于 website
文件夹中,我的入口点是 y.html
。
设置静态文件目录(您的网站页面),然后为根请求
提供 .html
示例项目:https://github.com/ondbyte/website
最后,运行 打开终端并移动到根文件夹。然后做
npm init
npm install express --no-save
node app.js
我有一个使用 Firebase 的云 Firestore 的 Flutter 应用程序。我已经完成了 Web 构建,并且通过 Android Studio 在 Chrome 上 运行 运行良好。我想与我的客户分享我的 Web 应用程序进度,但不想托管它(因为它尚未完成)。因此,我想找到一种在本地 运行 它的方法,就像使用 Android Studio 一样,但不需要安装 Android Studio(希望不需要安装 flutter要么),这样我就可以将构建文件发送给我的客户,他们可以 运行 在他们的机器上(使用脚本在本地启动网络服务器和 运行 网络应用程序)。
我尝试了包含在 web 构建文件夹中的以下脚本(index.html 所在的位置)
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from httplib import HTTPResponse
from os import curdir,sep
#Create a index.html aside the code
#Run: python server.py
#After run, try http://localhost:8080/
class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/':
self.path = '/index.html'
try:
sendReply = False
if self.path.endswith(".html"):
mimeType = 'text/html'
sendReply = True
if sendReply == True:
f = open(curdir + sep + self.path)
self.send_response(200)
self.send_header('Content-type', mimeType)
self.end_headers()
self.wfile.write(f.read())
f.close()
return
except IOError:
self.send_error(404,'File not found!')
def run():
print('http server is starting...')
#by default http server port is 80
server_address = ('127.0.0.1', 8080)
httpd = HTTPServer(server_address, RequestHandler)
try:
print 'http server is running...'
httpd.serve_forever()
except KeyboardInterrupt:
httpd.socket.close()
if __name__ == '__main__':
run()
但是当在 Chrome 上打开 http://localhost:8000
时,我得到一个空白页面并且控制台显示错误:
Failed to load resource: net::ERR_EMPTY_RESPONSE main.dart.js:1
Failed to load resource: net::ERR_EMPTY_RESPONSE manifest.json:1
Failed to load resource: net::ERR_EMPTY_RESPONSE :8080/favicon.png:1
我还通过 运行ning ws --spa index.html
尝试了 NPM local-web-server,但只是得到了 ERR_EMPTY_RESPONSE
响应。
这是我 build/web
在 运行 宁 flutter build web
:
如何创建一个本地服务器,我可以在其中托管我的网络应用程序并在本地 运行 它而不是在 Internet 上托管它?
正如你在评论中提到的那样。
使用以下内容创建文件 app.js
:
const express = require('express')
const app = express()
const port = 8000
app.get('/', (req, res) => {
console.log('getting request')
res.sendFile('website/y.html',{root:__dirname})
})
app.use(express.static(__dirname + '/website'))
app.use((req, res)=>{
res.redirect('/')
})
app.listen(port, () => {
console.log(`app listening at http://localhost:${port}`)
})
我的网站文件位于 website
文件夹中,我的入口点是 y.html
。
设置静态文件目录(您的网站页面),然后为根请求
示例项目:https://github.com/ondbyte/website
最后,运行 打开终端并移动到根文件夹。然后做
npm init
npm install express --no-save
node app.js