美文网首页
nodejs服务端开发基本配置

nodejs服务端开发基本配置

作者: 板栗炖牛肉 | 来源:发表于2021-11-04 14:29 被阅读0次

前言

  • nodejs服务端开发
  • 版本nodev14.15.5,npm6.14.11
  • 开发一个本地服务,下载输入命令即用

解决方案

  • 初始化一个项目npm init

  • 项目目录


    image.png
  • 配置端口以及请求方法,多余方法不贴方法,自行去掉,仅做参考,当前文件api.js
const {config} = require('../config');
const wordUtil = require('./util/wordUtil');  //不贴出,只做示范演示,自行去掉
const RestBean = require('./util/restBean');
const express = require('express');
const bodyParser = require('body-parser');
const app = express();

//跨域设置
app.all('*',function (req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', '*');
    res.header('Access-Control-Allow-Methods', '*');
    res.header('Access-Control-Allow-Credentials',true);
    next();
});


//使用RestFul风格
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));

process.on('uncaughtException', function (err) {
    console.error('全局异常捕获: ' + err);
});

/**
 * 运行监听
 */
const server = app.listen(config.port, function () {
    console.info(`服务已启动...\nhttp://localhost:${config.port}`)
});

/**
 * 运行标志
 */
app.get('/', function (req, res) {
    res.send('服务已运行')
});

/**
 * 下载文档
 */
app.post("/downWord", function (req, res) {
    try {
        const param = req.body;
        let {filename = paramError("参数异常"), userId = paramError("参数异常"), url = paramError("参数异常")} = param;
        wordUtil.downLoad(url, filename, userId, param.open, function () {
            res.send(RestBean.build(true));
        });
    } catch (e) {
        console.error(e);
        res.send(RestBean.build(null, -1, "异常:" + e));
    }
});


/**
 * 上传文档
 */
app.post("/upload", function (req, res) {
    try {
        const param = req.body;
        let {filename = paramError("参数异常"), userId = paramError("参数异常")} = param;
        wordUtil.upload(filename, userId, param.delete, res);
    } catch (e) {
        console.error(e);
        res.send(RestBean.build(null, -1, "异常:" + e));
    }
});

/**
 * 自定义、异常
 * @param str
 */
function paramError(str) {
    throw new Error(str)
}

  • 创建restful工具文件restBean可省略

module.exports = {
    build: build
};

/**
 * 构建
 * @param data
 * @param result
 * @param message
 */
function build(data, result, message) {
    return {
        result: result || 0,
        message: message || '成功',
        data
    };
}

  • 创建入口文件index.js
const api = require('./src/api'); //自定义目录

  • 重点!创建命令脚手架,npm下载安装完成后会通过该文件启动程序,cli.js,请全部复制#!/usr/bin/env node勿漏
#!/usr/bin/env node

require('./index.js');

  • 重点!package.json配置
{
  "name": "name",
  "version": "1.1.4",
  "main": "index.js",    //重要
  "license": "Creative Commons",
  "bin": {   //重要
    "klzy": "./cli.js"  //klzy为下载后执行命令
  },
  "files": [
    "src",
    "config.js",
    "index.js",
    "cli.js"
  ],
  "scripts": {
    "start": "node index.js"
  },
  "dependencies": {
    "body-parser": "1.19.0",
    "child_process": "1.0.2",
    "express": "4.17.1",
    "path": "0.12.7",
    "request": "2.88.2"
  }
}

  • 下载后npm全局路径执行命令
    image.png
image.png

相关文章

网友评论

      本文标题:nodejs服务端开发基本配置

      本文链接:https://www.haomeiwen.com/subject/llhkzltx.html