Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- app.use((req,res,next) => {
- res.header('Access-Control-Allow-Origin','*')
- res.header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept, Authorization')
- if(req.method === 'OPTIONS'){
- res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE , GET')
- return res.status(200).json({})
- }
- })
- const express = require('express')
- const cors = require('cors')
- const bodyParser = require("body-parser")
- const morgan = require('morgan')
- const app = express()
- const productRoutes = require('./routes/products')
- const ordersRoutes = require('./routes/orders')
- app.use(morgan('combined'))
- app.use(bodyParser.urlencoded({extended : false}))
- app.use(bodyParser.json())
- app.use(cors())
- //headers
- app.use((req,res,next) => {
- res.header('Access-Control-Allow-Origin','*')
- res.header('Access-Control-Allow-Headers','Origin, X-Requested-With, Content-Type, Accept, Authorization')
- if(req.method === 'OPTIONS'){
- res.header('Access-Control-Allow-Methods', 'PUT, POST, PATCH, DELETE , GET')
- return res.status(200).json({})
- }
- })
- //--headers
- app.use('/products',productRoutes)
- app.use('/orders',ordersRoutes)
- //Обработочка ошибочек
- app.use((req,res,next) => {
- const error = new Error('Not found')
- error.status = 404
- next(error)
- })
- app.use((error,req,res,next) => {
- res.status(error.status || 500)
- res.json({
- error:{
- message:error.message
- }
- })
- })
- //Обработочка ошибочек--
- app.listen(process.env.PORT || 8081 )
Add Comment
Please, Sign In to add comment