POST请求在localhost上工作,但无法POST到AWS

时间:2017-09-23 07:33:27

标签: node.js mongodb express mean-stack

我无法解决我的问题。我坚持这个问题很长一段时间了,所以现在我问你。 在我发布之前,这是my question

我犯了一个错误,那时我没有导出模块。 现在,我在localhost中测试过,效果很好。

enter image description here

但是,如果我使用邮递员进行测试,它就像bellow

一样无法正常工作

enter image description here

如果你们有这种经验并且已经解决了,那么请给我一些解决这个问题的技巧。

这是我的app.js,routes / imgFiles.js和models / imgFiles.js

app.js

const express = require('express');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 3000;

const path = require('path');
const favicon = require('serve-favicon');
const logger = require('morgan');
const cookieParser = require('cookie-parser');
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, '../awesome-drill/dist')));

app.use(logger('dev'));

const appRoutes = require('./routes/app');
const imgRoutes = require('./routes/imgFiles');
const userRoutes = require('./routes/user');

app.use(imgRoutes);
app.use(userRoutes);
app.use(appRoutes);

app.use(cookieParser());

app.use((req, res) => {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-Width, Content-Type, Accept');
    res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PATCH, DELETE, OPTIONS');
});

app.use(express.static('routes'));

app.get('*', (req, res) => {
    res.sendFile(path.join(__dirname, '../awesome-drill/dist/index.html'));
});

app.listen(port, () => {
    console.log(`Server is up on port ${port}`);
});

模型/ imgFiles.js

var express = require('express');
var router = express.Router();

var ImageFile = require('../models/imageFiles');

var multer = require('multer');
var storage = multer.diskStorage({
    destination: function (req, file, cb) {
        cb(null, '../img/') // Set directory
    },
    filename: function (req, file, cb) {
        cb(null, file.name) // Set file name
    }
});
var img = multer({ storage: storage });

router.post('/uploadFiles',img.single('imgfile'), (req, res) => {
    var imageFile = new ImageFile({
        imgName: req.body.imgName,
        imgType: req.body.imgType,
        imgSize: req.body.imgSize,
        imgContent: req.body.imgContent
    });

    imageFile.save((err, result) => {
        if (err) {
            return res.status(500).json({
                title: 'An error occured',
                error: err
            });
        }
        res.status(201).json({
            message: 'Img saved',
            obj: result
        });
    });
});

module.exports = router;

模型/ imgFiles.js

var {mongoose} = require('../db/mongoose');
var uniqueValidator = require('mongoose-unique-validator');

var schema = mongoose.Schema({
    imgName: {
        type: String,
    },

    imgType: {
        type: String,
    },

    imgSize: {
        type: Number,
    },

    imgContent: {
        type: String,
    },
});

schema.plugin(uniqueValidator);

module.exports = mongoose.model('ImageFile', schema);

0 个答案:

没有答案