Server Side Swift with Vapor - Part 2: Hello World | Ray Wenderlich

Edit: I have a feeling Iā€™m doing something wrong, I keep redoing the tutorial and getting new errors. Iā€™ll try work out what Iā€™m doing wrong sorry.

@0xtim Can you please help with this when you get a chance? Thank you - much appreciated! :]

Yes sorry, all sorted and got it working now, I just confused myself with the ā€œHelloVaporā€ project and ā€œhelloā€ page. Should have watched to the end of the vid first.

1 Like

Can I build my Vapor 3 project on Ubuntu ECS ?

I couldnā€™t make it work in a browser until I tried the solution found in this thread:

services.register { _ in 
  EngineServerConfig.detect(hostname: "127.0.0.1", port: 8080) 
}

Now it works with this port. Can anyone help me figure out whatā€™s wrong and why localhost:8080 doesnā€™t work?

@dricard have you got anything weird in your /etc/hosts file?

Iā€™m sure I donā€™t know whatā€™s supposed to be in there. Here whatā€™s in it.

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost

Anything wrong with that?

@dricard bizarre! That looks completely valid :thinking:

If you run ping localhost in terminal does it work? If you remove the solution you added and try running your application what gets printed in the console?

Yes if I ping localhost it works. When I remove the solution and I run this is what I get in the console:

Migrating sqlite DB
Migrations complete
Server starting on http://localhost:8080

And when I load http://localhost:8080/hello I get this in Safari:

It does the same in Firefox:

With the solution:

Migrating sqlite DB
Migrations complete
Server starting on http://127.0.0.1:8080

And loading http://127.0.0.1:8080/hello in Safari works (I get ā€˜Hello Worldā€™).

That is bizarre! Does a curl -v http://localhost:8080/hello work at all?

Speaking to some others it may be an IPv6 issue - this should be fixed by the integration of Swift NIO soon

This is what I get from the curl:

*   Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /hello HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
* Empty reply from server
* Connection #0 to host localhost left intact
curl: (52) Empty reply from server

Trying with the solution returns this instead:

*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 8080 (#0)
> GET /hello HTTP/1.1
> Host: 127.0.0.1:8080
> User-Agent: curl/7.54.0
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Length: 13
< Content-Type: text/plain; charset=utf-8
< Date: Wed, 07 Mar 2018 17:55:05 GMT
<
* Connection #0 to host 127.0.0.1 left intact
Hello, world!

@dricard ok so itā€™s definitely preferring IPv6 over IPv4 - looks like thereā€™s an issue around packets/streams for IPv6. The good news is this should be fixed pretty soon. As for why it is happening - if you add another line at the bottom of your /etc/hosts for 127.0.0.1 localhost to see if it picks that over IPv6 address - or wait for the fix with your workaround til then!

@0xtim thanks for looking into this for me! I guess Iā€™ll keep the workaround until this is resolved. Where should I look for info on a possible fix?

@dricard either the Vapor Slack or GitHub releases. Iā€™ll ping you when NIO hits though anyway

@0xtim hello budā€¦im running into a issue. everything is installed, Xcode toolchains is switched but it will not allow me to run the ā€œvapor new HelloVapor --branch=betaā€.

What is the error message you get? Knowing the error message could be useful in figuring out what is going on ā€¦

1 Like

got it working bud, TY though!

1 Like

Iā€™ve got 4.1 installed and vapor, however I canā€™t get the web server to launch. Iā€™ve deleted and recreated the app several times. I get the following error:

[ INFO ] Migrating ā€˜sqliteā€™ database (FluentProvider.swift:28)
[ INFO ] Preparing migration ā€˜Todoā€™ (MigrationContainer.swift:50)
:warning: [FluentError.reflectProperty: No property reflected for Swift.WritableKeyPath<App.Todo, Swift.Optional<Swift.Int>>] [/Users/maxfred/code/swift/vapor/HelloVapor/.build/checkouts/fluent.git-6251908308727715749/Sources/Fluent/Migration/Migration.swift:28:124]
Program ended with exit code: 1

@midtownkc run vapor update there was a broken reflection API in Core that for some reason still is being used by the template - Iā€™ll get that updated