Introduction: To implement a KOA service startup, followed by mock data links to the project, local code debugging, to achieve fast page effect, front-end and back-end coordination
1. Download the KOA package
npm install -D koa
Copy the code
The main implementation of the use of the core method: using the Node OS module, find the specific network address, local service forwarding of the project
var os = require("os");
// Returns an object containing the network interface for which the network address has been assigned
var ifaces = os.networkInterfaces();
Copy the code
/* * @title: using Koa to build Node services * @description: description */
const Koa = require("koa");
const router = require("koa-router") ();// Introduces koA-BodyParser middleware, which returns THE parameters of post requests in JSON format
const bodyParser = require('koa-bodyparser');
// With the middleware, we can use ctx.request.body to get the POST request parameters, which the middleware automatically interprets as JSON
var os = require("os");
// Returns an object containing the network interface for which the network address has been assigned
var ifaces = os.networkInterfaces();
const PORT = 8008;
let ipAddress = "0.0.0.0";
// Get the object address
Object.keys(ifaces).forEach(function(ifname) {
var alias = 0;
ifaces[ifname].forEach(function(iface) {
if ("IPv4"! == iface.family || iface.internal ! = =false) {
Skip over internal (i.e. 127.0.0.1) and non-ipv4 addresses
return;
}
if (alias >= 1) {
// this single interface has multiple ipv4 addresses
// console.log(ifname + ":" + alias, iface.address);
ipAddress = iface.address;
} else {
// this interface has only one ipv4 adress
// console.log(ifname, iface.address);
ipAddress = iface.address;
}
++alias;
});
});
console.log("ipAddress",ipAddress);
// Create a server
const app = new Koa();
app.use(bodyParser());
app.use(async (ctx, next) => {
ctx.set("Access-Control-Allow-Origin"."*"); // Specifies the outer domain URI that allows access to the resource
console.log(` service${ctx.request.method} ${ctx.request.url}. `);
await next();
});
router.get("/".async (ctx, next) => {
ctx.response.body = {
a: 1.b: "123"}; });/ / redirection
router
.all("/".(ctx, next) = > {
const url = `http://${ipAddress}:${PORT}/api/queryNumList`;
ctx.body = <a href=" <a href="${url}" target="_blank">${url}</a> </p> <p>please check your url!!! </p> `;
})
/ / case
router.get('/api/employees'.async (ctx, next) => {
ctx.response.body = {
status:true.data:[
{ projecttype: "Project Order Implementation".totalCount: "26" },
{ projecttype: "Before".totalCount: "2" },
{ projecttype: "Project Order Implementation".totalCount: "2" },
{ projecttype: "Post evaluation".totalCount: "23" },
{ projecttype: "Project Order Implementation".totalCount: "26" },
{ projecttype: "Operations".totalCount: "62" },
{ projecttype: "Trial run".totalCount: "2"}].msg:'Data obtained successfully'}}); app.use(router.routes()).use(router.allowedMethods()); app.listen(9000.() = > {
console.log(The node service has started. Please visit localhost:9000);
});
Copy the code