In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to write interface in nodejs". In daily operation, I believe many people have doubts about how to write interface in nodejs. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to write interface in nodejs". Next, please follow the editor to study!
In nodejs, an interface is a reference type that contains methods, properties, indexers, and events that are implemented using nodejs to define contracts as members. Nodejs can write interfaces through the express module.
This article operating environment: Windows10 system, nodejs version 12.19.0, Dell G3 computer.
What does the nodejs interface mean?
An interface (software class interface) is a reference type that defines a contract. Other types implement interfaces to ensure that they support certain operations. Interface specifies the member that must be provided by the class or other interface that implements it. Like classes, interfaces can contain methods, properties, indexers, and events as members.
The nodejs interface is a reference type that uses a nodejs implementation that contains methods, properties, indexers, and events to define the contract as members. Nodejs can write the interface through the express module.
How does nodejs write interfaces?
You can use the express framework to write an interface with moment (time formatting) middleware + cors middleware (cross-domain) + body-parser (parsing form) + mysql middleware.
Express is a flexible Node.js Web application development framework that keeps the minimum scale, providing a powerful set of features for Web and mobile applications. Node can connect objects to operate using methods such as app.get and app.post in the express framework.
Go directly to the code:
Const express = require ('express'); const app = express (); const moment = require (' moment') / / Import cors module, which is a cross-domain const cors = require ('cors'); app.use (cors ()); / / plug-in for parsing forms const bodyParser = require (' body-parser'); app.use (bodyParser.urlencoded ({extended: false})) / / create database connection object const mysql = require ('mysql') Const conn = mysql.createConnection ({host: '127.0.0.1 database address user:' root',// account password: 'root',// password database:' mydb_12_9',// library name multipleStatements: true / / allow multiple statements})
Data in the get table:
App.get ('/ api/getlist', (req, res) = > {const sqlStr = 'select * from zdcx' conn.query (sqlStr, (err, results) = > {if (err) return res.json ({err_code: 1, message: 'data does not exist', affextedRows: 0}) res.json ({err_code: 200, message: results, affextedRows: results.affextedRows})})
Query by condition:
App.get ('/ api/getlistdetl', (req, res) = > {const number= req.query.number const sqlStr = 'select * from zdcx where number=?' Conn.query (sqlStr, number, (err, results) = > {if (err) return res.json ({err_code: 1, message: 'data does not exist', affextedRows: 0}) res.json ({err_code: 200, message: results, affextedRows: results.affextedRows})})
Add data:
/ / add app.post ('/ api/addcard', (req, res) = > {const user = req.body user.ctime = moment (). Format ('YYYY-MM-DD HH:mm:ss') / / format date const sqlStr =' insert into bank set?' Conn.query (sqlStr, user, (err, results) = > {if (err) return res.json ({err_code: 1, message: err, affectedRows: 0}) res.json ({err_code: 0, message: 'Congratulations', affectedRows: results.affectedRows})})
Port snooping:
App.listen (3000, () = > {console.log ('listening port 3000, http://192.168.1.114:3000'); / / 192.168.1.114 replace your ip, native ip query cmd= > ipconfig})
Just run it and node your file name directly.
At this point, the study on "how to write the interface in nodejs" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.