Home > Web Front-end > Front-end Q&A > What does nodejs interface refer to?

What does nodejs interface refer to?

WBOY
Release: 2022-03-04 15:09:33
Original
2460 people have browsed it

In nodejs, an interface is a reference type implemented using nodejs that contains methods, properties, indexers and events as members to define a contract. Nodejs can write interfaces through the express module.

What does nodejs interface refer to?

The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.

What does the nodejs interface refer to?

Interface (software class interface) refers to the reference type that defines the agreement. Other types implement interfaces to ensure that they support certain operations. An interface specifies the members that must be provided by a class or other interface that implements it. Similar to classes, interfaces can contain methods, properties, indexers, and events as members.

The nodejs interface is a reference type implemented using nodejs that contains methods, properties, indexers and events as members to define the contract. Nodejs can write interfaces through the express module.

How to write an interface in nodejs?

You can use the express framework and cooperate with moment (time formatting) middleware cors middleware (for cross-domain use) body-parser (parsing form) mysql middleware to write an interface.

Express is a flexible Node.js web application development framework kept to a minimum, providing a powerful set of features for web and mobile applications. Node can use methods such as app.get and app.post in the express framework to connect objects for operations.

Go directly to the code:

const express = require('express');
const app = express();
const moment = require('moment')
    //导入cors模块,该模块为跨域所用
const cors = require('cors');
app.use(cors());
//解析表单的插件
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: false }))
//创建数据库连接对象
const mysql = require('mysql');
const conn = mysql.createConnection({
    host: '127.0.0.1',//数据库地址
    user: 'root',//账号
    password: 'root',//密码
    database: 'mydb_12_9',//库名
    multipleStatements: true //允许执行多条语句
})
Copy after login

get the data in the 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: '资料不存在', affextedRows: 0 })
        res.json({ err_code: 200, message: results, affextedRows: results.affextedRows })
    })
})
Copy after login

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: '资料不存在', affextedRows: 0 })
        res.json({ err_code: 200, message: results, affextedRows: results.affextedRows })
    })
})
Copy after login

Add data:

//添加
app.post('/api/addcard', (req, res) => {
    const user = req.body
    user.ctime = moment().format('YYYY-MM-DD HH:mm:ss')//格式化日期
    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: '恭喜成功', affectedRows: results.affectedRows })
    })
})
Copy after login

Port monitoring:

app.listen(3000, () => {
    console.log('正在监听端口3000,http://192.168.1.114:3000'); 
    //192.168.1.114换成你的ip,本机ip查询用cmd=>ipconfig
})
Copy after login

Just run node directly and your file name will do

Recommended learning: "nodejs video tutorial"

The above is the detailed content of What does nodejs interface refer to?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template