     res.header 'Access-Control-Allow-Methods', 'GET'
 
     CozyInstance.first (err, instance) ->
-        return next err if err?
-        unless instance?.domain
+        domain = instance?.domain or req.headers.host
+        unless domain
             return next new Error "Cozy's domain has not been registered"
-
-        host = 'https://' + instance.domain
-        template = "#{host}/webfinger/json?resource={uri}"
+        template = "https://#{domain}/webfinger/json?resource={uri}"
 
         hostmeta = links:
             rel: 'lrdd'

# Return the host meta file
# support only JSON format
# @TODO : support xml
module.exports.webfingerHostMeta = (req, res) ->

    return res.sendStatus 404 unless req.params.ext is 'json'


module.exports.webfingerAccount = (req, res, next) ->

    CozyInstance.first (err, instance) ->
        domain = instance?.domain or req.headers.host
        unless domain
            return next new Error "Cozy's domain has not been registered"
        host = "https://#{domain}"

        if req.params.module in ['caldav', 'carddav']
            routes = router.getRoutes()
            if routes['sync']?
