dpape-node-service
v1.0.7
Published
Nodejs for server-side development integration
Downloads
2
Maintainers
Readme
dpape-node-service
Nodejs for server-side development integration
Install
npm install dpape-node-service -S
# OR
yarn add dpape-node-service -S
Initial Configuration
import { init } from "dpape-node-service";
import { join } from "path";
init({
// mysql config items
mysql: {
user: "root",
password: "**********",
database: "databaseName",
},
// mysql mapper xml path
mapperBasePath: join(__dirname, "./mapper"),
// Save log address
logs: join(__dirname, "./logs"),
// mongodb config items
mongodb: {
user: "mongo",
password: "*********",
database: "databaseName",
authDatabase: "admin",
},
});
Quick Start
// index.js
import service, { app } from "dpape-node-service";
import { join } from "path";
const userApi = app();
// Last access path `/api/user/info`
userApi.get("/info", (req, res) => {
res.send({
statusCode: 200,
data: {
name: "admin",
age: 25,
},
});
});
const apis = [
{
path: "/user",
app: userApi,
},
];
service({
staticConfig: {
url: "/",
address: join(__dirname, "./static"),
},
apiPrefix: "/api",
apis: apis,
port: 8080,
});
Run the script
- node index.js
- Service app listening at http://localhost:8080
Use MySql database
Create the XML mapping file./mapper/xxx.xml
<?xml version="1.0" encoding="UTF-8"?>
<mapper>
<item key="product_name" as="productName"></item>
<item key="product_intro" as="productIntro"></item>
<item key="id" as="productId"></item>
<sql type="select" name="getProductInfo">
select product_name,product_intro from product where id = #{id}
</sql>
</mapper>
Invoke the defined method. The name attribute in.xml
import { sqlMapper } from "dpape-node-service";
async () => {
try {
const result = await sqlMapper("xxx.xml", "getProductInfo");
return result;
} catch (err) {
return Promise.reject(err);
}
};
Read on for more practical tips nodejs-mysql-mapper
Use MongoDB database
import { mongo } from "dpape-node-service";
//Query
async () => {
try {
const result = await mongo.querys("collectionName");
return result;
} catch (err) {
return Promise.reject(err);
}
};
//Insert
async () => {
try {
const result = await mongo.inserts("collectionName", {
name: "test name",
id: 1,
});
return result;
} catch (err) {
return Promise.reject(err);
}
};
//Update
async () => {
try {
const result = await mongo.updates(
"collectionName",
{ id: 1 },
{
name: "update - test name",
}
);
return result;
} catch (err) {
return Promise.reject(err);
}
};
Logs record
import { log } from "dpape-node-service";
// info logs
log.info("This is an info log");
// error logs
log.info("This is an error log");
Logs Output (in info.log and error.log):
# error.log
[2021-05-26T15:31:41.276] [ERROR] error - This is an error log
# info.log
[2021-05-26T15:31:41.274] [INFO] info - This is an info log
init Api
| Name | Describe | Default Value | Required | | :------------: | :------------------------: | :-----------: | :------: | | mysql | MySQL config info | -- | false | | mapperBasePath | mysql mapper xml path | -- | false | | mongodb | mysql mongodb config items | -- | false | | logs | Save log address | -- | false |
Start service Api
| Name | Describe | Default Value | Required | | :----------: | :---------------------------------------------------------------: | :-----------: | :------: | | port | Service port | -- | true | | apis | Web Service List for express app | -- | false | | apiPrefix | api prefix config | /api | false | | cross | Support cross-domain | false | false | | staticConfig | Static resource address. Supports one or more configuration items | -- | false | | headers | Custom request header information | -- | false |
Frequently Asked Questions
Q: Connect MySQL Error
ER_NOT_SUPPORTED_AUTH_MODE: Client does not support authentication protocol requested by server;
A: Please execute the following command in MySQL
ALTER USER '[username]'@'[ip]' IDENTIFIED WITH mysql_native_password BY '[password]';