热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何部署和配置节点js托管?

如何配置节点js托管?。我已通过filezilla将文件上传到服务器。在Postman中

如何配置节点js托管?。我已通过filezilla将文件上传到服务器。在Postman中,当我在网站上使用POST方法和http: // localhost: 8080 / api / contacts(我在mongodb中上传文件)时,可以看到添加的联系人。我的域名是http://aplik11.usermd.net,即所选节点js类型。在myfiles选项卡中,选择域application1.usermd.net。里面是pubic_nodejs文件夹。我要做的就是将api-routes.jscontactController.jscontactModel.jsindex.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);
});





推荐阅读
  • 了解如何快速搭建属于自己的个人博客,无需编程基础,适合Mac和Windows用户。通过本文,您将学会使用GitHub Pages和Hexo构建一个完全自主的在线空间。 ... [详细]
  • 使用Python在SAE上开发新浪微博应用的初步探索
    最近重新审视了新浪云平台(SAE)提供的服务,发现其已支持Python开发。本文将详细介绍如何利用Django框架构建一个简单的新浪微博应用,并分享开发过程中的关键步骤。 ... [详细]
  • 本文详细介绍了 Dockerfile 的编写方法及其在网络配置中的应用,涵盖基础指令、镜像构建与发布流程,并深入探讨了 Docker 的默认网络、容器互联及自定义网络的实现。 ... [详细]
  • PostgreSQL 10 离线安装指南
    本文详细介绍了如何在无法联网的服务器上进行 PostgreSQL 10 的离线安装,并涵盖了从下载安装包到配置远程访问的完整步骤。 ... [详细]
  • 本文探讨了如何在Node.js环境中,通过Tor网络使用的SOCKS5代理执行HTTP请求。文中不仅提供了基础的实现方法,还介绍了几种常用的库和工具,帮助开发者解决遇到的问题。 ... [详细]
  • 本文深入探讨了 Prototype.js 框架及其与 JavaScript 原生 toString() 方法之间的区别,适合对前端开发感兴趣的开发者阅读。文章将帮助读者理解两者在功能实现和应用场景上的不同,从而更好地利用这些工具进行高效编程。 ... [详细]
  • golang常用库:配置文件解析库/管理工具viper使用
    golang常用库:配置文件解析库管理工具-viper使用-一、viper简介viper配置管理解析库,是由大神SteveFrancia开发,他在google领导着golang的 ... [详细]
  • [论文笔记] Crowdsourcing Translation: Professional Quality from Non-Professionals (ACL, 2011)
    Time:4hoursTimespan:Apr15–May3,2012OmarZaidan,ChrisCallison-Burch:CrowdsourcingTra ... [详细]
  • 优化联通光猫DNS服务器设置
    本文详细介绍了如何为联通光猫配置DNS服务器地址,以提高网络解析效率和访问体验。通过智能线路解析功能,域名解析可以根据访问者的IP来源和类型进行差异化处理,从而实现更优的网络性能。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 网络运维工程师负责确保企业IT基础设施的稳定运行,保障业务连续性和数据安全。他们需要具备多种技能,包括搭建和维护网络环境、监控系统性能、处理突发事件等。本文将探讨网络运维工程师的职业前景及其平均薪酬水平。 ... [详细]
  • 解决Element UI中Select组件创建条目为空时报错的问题
    本文介绍如何在Element UI的Select组件中使用allow-create属性创建新条目,并处理创建条目为空时出现的错误。我们将详细说明filterable属性的必要性,以及default-first-option属性的作用。 ... [详细]
  • 本文探讨了如何在 PHP 的 Eloquent ORM 中实现数据表之间的关联查询,并通过具体示例详细解释了如何将关联数据嵌入到查询结果中。这不仅提高了数据查询的效率,还简化了代码逻辑。 ... [详细]
  • 深入解析TCP/IP五层协议
    本文详细介绍了TCP/IP五层协议模型,包括物理层、数据链路层、网络层、传输层和应用层。每层的功能及其相互关系将被逐一解释,帮助读者理解互联网通信的原理。此外,还特别讨论了UDP和TCP协议的特点以及三次握手、四次挥手的过程。 ... [详细]
  • Hybrid 应用的后台接口与管理界面优化
    本文探讨了如何通过优化 Hybrid 应用的后台接口和管理界面,提升用户体验。特别是在首次加载 H5 页面时,为了减少用户等待时间和流量消耗,介绍了离线资源包的管理和分发机制。 ... [详细]
author-avatar
HurricaneCC
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有