Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

193
Visualizações
Swift simple Kitura app localhost not running?

first post!

I'm a student learning swift and I'm on a rather big crash course. My professor started teaching us how to do simple get and post requests last time, but I'm still catching up and very clearly haven't grasped some of the basics.

This is my first time working with Kitura and only the second time coding in Swift. For some reason when I use swift run, I get my print and a sudden "Program ended with exit code : 0" rather than having a localhost running on my 8080 port to verify my get response on localhost:8080/.

Could someone help me figure out what I'm not seeing? Or haven't understood about server-side swift and command lines?

print("Hello, world from Swift Main!")

import Kitura

//constant router
let router = Router()


//When the router gets a request (contains everything needed to interpret the request), the server will respond with (Hello World or whatever data)
router.get("/") { request, response, next in
    response.send("Hello world from router.get") //response
    next() //either end the route or go on to the next one
}

//What port for the server to run on
Kitura.addHTTPServer(onPort: 8080, with: router)


//Need to add routes before run(), either in different file or on main
Kitura.run()

Thanks!

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

The code you have provided is correct, so it may be that your project structure is not.

In order to correctly create the project, you can follow the steps below:
1. Create a new directory, eg jamie.
2. From that directory run swift package init --type=executable to create a new project. The project will be called jamie after the directory name.
3. Edit Sources/jamie/main.swift and add your code.
4. Edit Package.swift and add the following to the dependencies of the "Jamie" package:
.package(url: "https://github.com/IBM-Swift/Kitura.git", .upToNextMajor(from: "2.5.0")),.
5. Edit Package.swift and add the as the list of dependencies for the "jamie" target:
dependencies: ["Kitura"]),

That should then run correctly. If it doesn't, it may be because the port you are trying to use is already in use. If you add a logger to your project, Kitura will log an error message if it cannot bind to the port. To do that follow the steps below:

  1. Edit Package.swift and add the following to the dependencies of the "Jamie" package:
    .package(url: "https://github.com/IBM-Swift/HeliumLogger.git", .upToNextMinor(from: "1.7.1")),.
  2. Edit Package.swift and add the logger to the list of dependencies for the "jamie" target so that it becomes:
    dependencies: ["Kitura", HeliumLogger"]),
  3. Edit Sources/jamie/main.swift and add the following to the top of the file:

    import LoggerAPI
    import HeliumLogger
    
    HeliumLogger.use(LoggerMessageType.info)
    

    If the port is already in use, you'll get a message similar to the following:

[2019-02-17T12:01:40.723Z] [ERROR] [Kitura.swift:139 start()] Error listening on port 8080: Error code: -9992(0x-2708), Address already in use. Use server.failed(callback:) to handle

If you haven't already, its probably worth joining the Kitura slack organisation as http://slack.kitura.io

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda