Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
140 views
in Technique[技术] by (71.8m points)

javascript - 无法使用Angular发布到CosmosDB(Cannot POST to CosmosDB using Angular)

I am trying to post to my cosmosDB using Angular.

(我正在尝试使用Angular发布到我的cosmosDB。)

I can GET just fine, but POST returns with a 404 error in Postman.

(我可以GET很好,但是POST在Postman中返回404错误。)

I am new to routes and APIs so I am a little lost on what is causing the issue.

(我是路由和API的新手,所以我对导致问题的原因有些困惑。)

Here is my index.js

(这是我的index.js)

const bodyParser = require('body-parser');
const path = require('path');
const routes = require('./routes');

const root = './';
const port = process.env.PORT || '3000';
const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(root, 'dist/checkin')));
app.use('/api', routes);
app.get('*', (req, res) => {
  res.sendFile('dist/checkin/index.html', {root});
});

app.listen(port, () => console.log(`API running on localhost:${port}`));

My routes.js

(我的routes.js)

const contactService = require('./contact.service');
const router = express.Router();

router.get('/contacts', (req, res) => {
   contactService.getContacts(req, res);
});

router.post('/contact/', (req, res) => {
   contactService.postContact(req, res);
});

module.exports=router;

My contact.service.js which contains all of my operations (Just GET and POST right now)

(我的contact.service.js包含我所有的操作(现在只需GET和POST))

const ReadPreference = require('mongodb').ReadPreference;

require('./mongo').connect();

function getContacts(req, res) {
  const docquery = Contact.find({}).read(ReadPreference.NEAREST);
  docquery
    .exec()
    .then(contacts => {
      res.status(200).json(contacts);
    })
    .catch(error => {
      res.status(500).send(error);
      return;
    });
}

function postContact(req, res) {
    const originalContact = { uid: req.body.uid, name: req.body.name, description: req.body.description };
    const contact = new Contact(originalContact);
    contact.save(error => {
      if (checkServerError(res, error)) return;
      res.status(201).json(contact);
      console.log('Contact created successfully!');
    });
  }

  function checkServerError(res, error) {
    if (error) {
      res.status(500).send(error);
      return error;
    }
  }

module.exports = {
  getContacts,
  postContact
};

Input is obtained through an HTML forum which is picked up and sent through

(输入是通过一个HTML论坛获得的,该论坛被拾起并通过发送)

    return this.http.post<Contact>(`${api}/contact/`, contact);
  }
  ask by tellux_ translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...