egg是阿里开发的一个强约束node框架,基于koa,所有koa插件都可以在egg上直接使用,详细请访问egg官网
安装开发工具
- 安装nodejs
- git
- mysql
- Navicat for MyAQL
- cnpm
npm i -g cnpm
配置开发环境
-
下载基础代码包
github地址
或直接clone
git clone https://github.com/adrianjiang/eggDemo
-
安装依赖
cnpm i
-
启动
npm run dev
-
打开浏览器
http://localhost:7001/
如果能够看到hi egg则说明配置成功
静态文件发布
放在app/public
下面的文件都可以通过http://localhost:7001/public/index.html访问到

配置路由
打开router.js文件

现在编写一个借口作为示范
- 设置路由地址
在router.js文件中添加app.get('/helloworld', 'helloworld.index');

- 编写控制器
在controller文件夹中创建文件helloworld.js

添加如下代码
exports.index = function* (ctx) {
ctx.body = {
name: 'hello world'
};
};

访问http://localhost:7001/helloworld

网友评论