美文网首页
Vapor-Swift语言服务器端学习三、运行Demo

Vapor-Swift语言服务器端学习三、运行Demo

作者: 谢顶强 | 来源:发表于2023-03-16 15:37 被阅读0次

1. 下载demo

cd vapor
# 将在当前目录下创建一个hello工程
vapor new hello -n
# 切换到hello目录
cd hello
# 将下载关联库,并编译,需要较长时间
vapor run
# 经较长时间下载,编译运行,终端显示如下
Server starting on http://127.0.0.1:8080

2.同步云端

# 将hello目录上传到云端已创建的swift目录下
scp -r xxx/vapor/hello ubuntu@xxx.xxx.x.xxx:~/swift/hello
# ssh链接云端,输入密码
ssh ubuntu@xxx.xxx.x.xxx
# 切换到工作目录
cd swift/hello
# 运行目录
vapor run
# 终端迅速编译后,显示如下
Server starting on http://127.0.0.1:8080

3.开放公网ip

由于127.0.0.1为本机地址,通过云主机的公网ip,不能访问到此地址。所以,我们需要将httpserver的hostname设置为0.0.0.0。

# 修改配置文件 hello/Sources/App/configure.swift代码如下
import Vapor

// configures your application
public func configure(_ app: Application) throws {
    // uncomment to serve files from /Public folder
    // app.middleware.use(FileMiddleware(publicDirectory: app.directory.publicDirectory))

    // register routes
    app.http.server.configuration.hostname = "0.0.0.0"
    try routes(app)
}
# 更新文件到云端后,关停云端重新运行
vapor run
# 编译运行后,显示如下
Server starting on http://0.0.0.0:8080

4.测试公网

打开浏览器或postman,输入http://xxx.xxx.xxx.xxx:8080,可见内容如下:

It works!

继续在浏览器或postman中,输入http://xxx.xxx.xxx.xxx:8080/hello,可见内容如下:

Hello, world!

相关文章

  • 第一章 php的基本语法

    #一,认识php 1,服务器端的语言 php是服务器端运行的语言,并且只能在服务器端运行,而不会传到客户端。只有当...

  • 1.PHP基本语法(一)

    php可以做什么 php是一种可以在服务器端运行的编程语言,可以运行在Web服务器端。 php是一门后台编程语言,...

  • php 介绍

    语言归类:服务器端脚本语言 全称:PHP:Hypertext Preprocessor 即:超文本标记语言 运行环...

  • Flutter

    搭环境 【Flutter 学习笔记三】Windows 安装 Flutter 并运行 DEMO - 袁超 - Seg...

  • PHP知识点

    Php:脚本语言,网站建设,服务器端运行 PHP定义:一种服务器端的HTML脚本/编程语言,是一种简单的、面向对象...

  • 4 Less Sass预处理语言(css模块化)

    Less预处理语言 ->向后兼容的CSS扩展语言,即可运行在浏览器端,也可运行在服务器端(Node.js环境); ...

  • PHP学习基础第一节

    一、php可以做什么 php是一种可以在服务器端运行的编程语言,可以运行在Web服务器端。 php是一门后台编程语...

  • shell script学习笔记

    shell学习笔记 什么是shell? shell是运行在linux服务器上的用c语言编写的程序,即可以是服务器端...

  • 2019-07-21

    学习PHP不得不懂的常识 1.PHP是什么? 运行再服务器端的HTML的脚本编程语言,用于书写动态生成的网页。 支...

  • php

    PHP简介 PHP是一种超文本预处理语言,运行在服务器端能和html进行嵌套用于制作动态网页的语言 特点 运行在服...

网友评论

      本文标题:Vapor-Swift语言服务器端学习三、运行Demo

      本文链接:https://www.haomeiwen.com/subject/hjppkdtx.html