Guest User

Untitled

a guest
May 25th, 2018
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.55 KB | None | 0 0
  1. app.use((req,res,next) => {
  2. res.header('Access-Control-Allow-Origin','*')
  3. res.header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept, Authorization')
  4. if(req.method === 'OPTIONS'){
  5. res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE , GET')
  6. return res.status(200).json({})
  7. }
  8. })
  9.  
  10. const express = require('express')
  11. const cors = require('cors')
  12. const bodyParser = require("body-parser")
  13. const morgan = require('morgan')
  14.  
  15. const app = express()
  16. const productRoutes = require('./routes/products')
  17. const ordersRoutes = require('./routes/orders')
  18.  
  19. app.use(morgan('combined'))
  20. app.use(bodyParser.urlencoded({extended : false}))
  21. app.use(bodyParser.json())
  22. app.use(cors())
  23. //headers
  24. app.use((req,res,next) => {
  25. res.header('Access-Control-Allow-Origin','*')
  26. res.header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept, Authorization')
  27. if(req.method === 'OPTIONS'){
  28. res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE , GET')
  29. return res.status(200).json({})
  30. }
  31. })
  32. //--headers
  33. app.use('/products',productRoutes)
  34. app.use('/orders',ordersRoutes)
  35. //Обработочка ошибочек
  36. app.use((req,res,next) => {
  37. const error = new Error('Not found')
  38. error.status = 404
  39. next(error)
  40. })
  41. app.use((error,req,res,next) => {
  42. res.status(error.status || 500)
  43. res.json({
  44. error:{
  45. message:error.message
  46. }
  47. })
  48. })
  49. //Обработочка ошибочек--
  50. app.listen(process.env.PORT || 8081 )
Add Comment
Please, Sign In to add comment