I have encountered a problem in restify. I can only use get-methods in the api. Normaly it seems like people have had problems when it comes to CORS, but I cant even use it with the same domain. I have tried alot and for a little while it work with the same domain at localhost. But then I modified my code a little and it didnt work. Also I could not seem to get the parameters in the req.body or req.params for my POST request. Im not using any preflight request either.
'use strict';
var restify = require('restify'),
plugins = require('restify-plugins'),
config = require('./config.js'),
personController = require('./routes/person-controller')
var server = restify.createServer({
name: config.name,
version: config.version,
})
server.use(plugins.jsonBodyParser({ mapParams: true }))
server.use(plugins.acceptParser(server.acceptable))
server.use(plugins.queryParser({ mapParams: true }))
server.pre(restify.CORS({
origins: [
'*'
],
headers: [
"authorization",
"withcredentials",
"x-requested-with",
"x-forwarded-for",
"x-real-ip",
"x-customheader",
"user-agent",
"keep-alive",
"host",
"accept",
"connection",
"upgrade",
"content-type",
"dnt",
"if-modified-since",
"cache-control",
"Accept-Encoding",
"Accept-Language",
"User-Agent",
"Accept",
"DNT",
"Connection",
"Upgrade-Insecure-Requests",
"Cache-Control",
"Pragma",
"Content-Length",
"Content-Type",
"Accept-Type"
],
methods: ["GET", "POST", "PUT"]
})
)
server.use(plugins.fullResponse())
server.get("/api/values", personController.readAll);
server.get("/api/values/:id", personController.readOne);
server.post("/api/values/", personController.createPerson);
server.put("/api/values/", personController.updatePerson);
server.del("/api/values/", personController.delPerson);
server.on('uncaughtException', (req, res, route, err) => {
log.error(err.stack)
res.send(err)
});
server.listen(config.port, function () {
})
Very greatful for help. This is pretty urgent and im thinking about switching to express if I dont solve it by tomorrow night.
via Sverker Söderlund
No comments:
Post a Comment