作者:HurricaneCC | 来源:互联网 | 2023-09-07 09:59
如何配置节点js托管?。我已通过filezilla
将文件上传到服务器。在Postman中,当我在网站上使用POST方法和http: // localhost: 8080 / api / contacts
(我在mongodb中上传文件)时,可以看到添加的联系人。我的域名是http://aplik11.usermd.net
,即所选节点js类型。在myfiles选项卡中,选择域application1.usermd.net
。里面是pubic_nodejs文件夹。我要做的就是将api-routes.js
,contactController.js
,contactModel.js
,index.js
上传到其中吗?是否仍需要以某种方式进行配置?对我来说,重要的是要引用Postman中的域名,而不是http: //http://aplik11.usermd.net / api / contacts
下的localhost。
API路由
// api-routes.js
// Initialize express router
let router = require('express').Router();
// Set default API response
router.get('/',function (req,res) {
res.json({
status: 'API Its Working',message: 'Welcome to RESTHub crafted with love!',});
});
// Import contact controller
var cOntactController= require('./contactController');
// Contact routes
router.route('/contacts')
.get(contactController.index)
.post(contactController.new);
router.route('/contacts/:contact_id')
.get(contactController.view)
.patch(contactController.update)
.put(contactController.update)
.delete(contactController.delete);
// Export API routes
module.exports = router;
contactController.js
// Import contact model
COntact= require('./contactModel');
// Handle index actions
exports.index = function (req,res) {
Contact.get(function (err,contacts) {
if (err) {
res.json({
status: "error",message: err,});
}
res.json({
status: "success",message: "Contacts retrieved successfully",data: contacts
});
});
};
// Handle create contact actions
exports.new = function (req,res) {
var cOntact= new Contact();
contact.name = req.body.name ? req.body.name : contact.name;
contact.gender = req.body.gender;
contact.email = req.body.email;
contact.phOne= req.body.phone;
// save the contact and check for errors
contact.save(function (err) {
// Check for validation error
if (err)
res.json(err);
else
res.json({
message: 'New contact created!',data: contact
});
});
};
// Handle view contact info
exports.view = function (req,res) {
Contact.findById(req.params.contact_id,function (err,contact) {
if (err)
res.send(err);
res.json({
message: 'Contact details loading..',data: contact
});
});
};
// Handle update contact info
exports.update = function (req,contact) {
if (err)
res.send(err);
contact.name = req.body.name ? req.body.name : contact.name;
contact.gender = req.body.gender;
contact.email = req.body.email;
contact.phOne= req.body.phone;
// save the contact and check for errors
contact.save(function (err) {
if (err)
res.json(err);
res.json({
message: 'Contact Info updated',data: contact
});
});
});
};
// Handle delete contact
exports.delete = function (req,res) {
Contact.remove({
_id: req.params.contact_id
},contact) {
if (err)
res.send(err);
res.json({
status: "success",message: 'Contact deleted'
});
});
};
contactModel.js
var mOngoose= require('mongoose');
// Setup schema
var cOntactSchema= mongoose.Schema({
name: {
type: String,required: true
},email: {
type: String,gender: String,phone: String,create_date: {
type: Date,default: Date.now
}
});
// Export Contact model
var COntact= module.exports = mongoose.model('contact',contactSchema);
module.exports.get = function (callback,limit) {
Contact.find(callback).limit(limit);
}
index.js
// Import express
let express = require('express');
// Import Body parser
let bodyParser = require('body-parser');
// Import Mongoose
let mOngoose= require('mongoose');
// Initialize the app
let app = express();
// Import routes
let apiRoutes = require("./api-routes");
// Configure bodyparser to handle post requests
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
// Connect to Mongoose and set connection variable
mongoose.connect('mongodb://XXXXX0:XXXXXXXx@mongo.mydevil.net:20000/XXXXX',{ useNewUrlParser: true });
var db = mongoose.connection;
// Added check for DB connection
if(!db)
console.log("Error connecting db")
else
console.log("Db connected successfully")
// Setup server port
var port = process.env.PORT || 8080;
// Send message for default URL
app.get('/',(req,res) => res.send('Hello World with Express'));
// Use Api routes in the App
app.use('/api',apiRoutes);
// Launch app to listen to specified port
app.listen(port,function () {
console.log("Running App on port " + port);
});