NodeJS 使用express创建服务器

本文详细介绍如何使用NodeJS和Express框架快速创建Web服务器。通过模块化思想组织代码,包括数据库连接、路由设置及服务器监听。示例代码涵盖db.js、router.js和app.js文件,演示从安装express到服务器启动的全过程。

NodeJS 使用express创建服务器

express是基于NodeJS基于平台,快速、开放、极简的 Web开发框架 文档说明
使用模块化思想进行代码书写
  1. 文档结构:db.js用于存放连接数据库的代码 router.js用于存放路由模块的代码 app.js用于存放创建服务器的代码

在这里插入图片描述
app.js代码 先要安装express 使用npm install express

const express = reqiure("express")
const app = express()
// 导入路由模块
const router = require("./router/router")
// 使用
app.use(router)

app.listen(9527,() => {
	console.log("localhost:9527  启动了")
})

db,js代码

const mysql = require("mysql")
const connection = mysql.createConnection({
    host: "localhost",
    user: "root",
    password: "root",
    database: "onepiece"
})

connection.connect()

module.exports = connection

router.js代码

const express = require("express")
const router = express.Router()

router.get("/",(request,response) => {
	response.send("hello world")
})
module.exports = router

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值