当前位置:网站首页>【实战】Next.js + 云函数开发一个面试刷题网站
【实战】Next.js + 云函数开发一个面试刷题网站
2022-08-03 14:23:00 【InfoQ】
前言
技术栈选择
- Next.js —— React 服务端渲染框架
- Tailwindcss —— CSS 原子类框架
- 云数据库 —— 和小程序公用一套数据库
项目初始化
create-next-appnpx create-next-app next-interview
cd next-interview
next-interviewtailwindcsstailwindcssyarn add --dev tailwindcss postcss autoprefixer @tailwindcss/typography
npx tailwindcss init -p
pagesstylessrctailwindcss.config.js/** @type {import('tailwindcss').Config} */
module.exports = {
content: [
"./src/**/*.{js,ts,jsx,tsx}",
],
theme: {
extend: {},
},
plugins: [require('@tailwindcss/typography')],
}
srccss@tailwindcss/typographystyles/globals.css@tailwind base;
@tailwind components;
@tailwind utilities;
index.jsimport Head from 'next/head';
export default function Home() {
return (
<div>
<Head>
<title>前端面试题库</title>
</Head>
<h1 className="text-3xl text-blue-500 flex p-10 justify-center">
前端面试题库
</h1>
</div>
);
}
yarn && yarn dev
云数据库
export interface Question {
_id: string;
category: string; // 分类
title: string; //标题
desc: string; // 简介
options?: string; //选项,选择题
explanation: string;// 答案解析
level: number; // 难度等级
tagId: number; // 标签
}
exports.main = async (event, context) => {
const db = uniCloud.database();
const page = event.page || 1;
const res = await db.collection('fe-question').skip((page - 1) * 20)
.limit(20)
.get();
//返回数据给客户端
return res.data
};
uniCloud.callFunction云函数 URL 化
设置云函数 HTTP 访问地址
- 登录 uniCloud 后台 (opens new window),选择服务空间。
- 单击左侧菜单栏【云函数】,进入云函数页面。
- 点击需要配置的云函数的【详情】按钮,配置访问路径。

path//functionName云函数的调用方式
context.SOURCE- client: 客户端
callFunction方式调用
- http: 云函数 url 化方式调用
- timing: 定时触发器调用
- server: 由管理端调用,
HBuilderX里上传并运行
- function: 由其他云函数
callFunction调用
'use strict';
exports.main = async (event, context) => {
let source = context.SOURCE // 当前云函数 Url 后, 为 http 的方式调用
}
云函数的入参
{
path: 'HTTP请求路径,如 /hello',
httpMethod: 'HTTP请求方法,如 GET',
headers: {HTTP请求头},
queryStringParameters: {HTTP请求的Query,键值对形式},
body: 'HTTP请求体',
isBase64Encoded: 'true or false,表示body是否为Base64编码'
}
GEThttps://${云函数Url化域名}/${functionPath}?a=1&b=2event{
path: '/',
httpMethod: 'GET',
headers: {HTTP请求头},
queryStringParameters: {a: "1", b: "2"},
isBase64Encoded: false
}
POSThttps://${spaceId}.service.tcloudbase.com/${functionPath}event.body{
path: '/',
httpMethod: 'POST',
headers: {
...
"content-type": 'application/json'
},
isBase64Encoded: false,
body: '{"a":1,"b":2}', // 注意此处可能是base64,需要根据isBase64Encoded判断
}
exports.main = async (event, context) => {
const db = uniCloud.database();
let source = context.SOURCE;
let data = event;
if ("http" === source) {
// data=event.queryStringParameters //get 方式获取参数
data = JSON.parse(event.body); // post 方式获取参数
}
const page = data.page || 1;
const res = await db.collection('fe-question').skip((page - 1) * 20)
.limit(20)
.get();
//返回数据给客户端
return res.data
};
服务端渲染
getServerSidePropsimport fetch from 'node-fetch';
function Page({ data }) {
// 渲染数据
return data.map((item)=><div key={item.id}>...</div>)
}
// 每次页面刷新都会执行这个方法
export async function getServerSideProps() {
// 从云函数请求数据
const res = await fetch(`https://.../data`)
const data = await res.json()
// 将返回的结果通过 props 传递给组件
return { props: { data } }
}
export default Page
getServerSidePropsnode-fetch部署

存在问题


getServerSideProps

小结
优点
- Vercel 完美的结合和 GitHub,部署极快
- Vercel 可以自动分配 Https 证书,我们可以添加自定义绑定域名,并且域名可以免备案
- 云开发选择阿里云,云数据库和云函数都免费
缺点
最后
- 前端工具箱[1]
- GitHub 代码[2]
边栏推荐
- LARS (Least Angle Regression)
- 想成为网络安全技术爱好者(可能是黑客)的话,需要看什么书?
- The Chinese Embassy in Nigeria issued an emergency safety warning for the area near Zuma Rock in Abuja
- PostgreSQL 每周新闻 2022-7-27
- 位级运算之提取位级表示的最高位
- PAT乙级-B1016 部分A+B(15)
- 使用alarm函数实现sleep,使用alarm函数实现对阻塞操作设置超时
- 大势,又一关乎中美竞争的关键行业,走到关键时刻了
- PCL 点云按时间进行分段
- 网络通信的过程
猜你喜欢
随机推荐
QImageReader
leetcode 448. Find All Numbers Disappeared in an Array 找到所有数组中消失的数字(简单)
HCIP Day 16 Notes (SVI, Spanning Tree Protocol)
How to connect a VMware virtual machine to the network "recommended collection"
SnapHelper翻页效果两行代码带你实现不一样的RecyclerView
MMA安装及使用优化
使用alarm函数实现sleep,使用alarm函数实现对阻塞操作设置超时
Role usage in Ansible
162_Power Query is a custom function for quickly merging tables in a folder TableXlsxCsv_2.0
MySQL【存储过程与函数】
GBase 8c数据库集群中,怎么替换节点呢?比如设置A节点为gtm,换到B节点上。
网络中的交换机和路由器
With 1000 lines of code statistics after the xi 'an housing prices, I have a startling discovery...
MATLAB中writetimetable函数用法
Left index of all anagrams in leetcode/string (some permutation of s1 string is a substring of s2)
图形学-粒子系统 (Particle System)
游戏版号“地下交易”,一个版号能卖上千万?
Redis学习
数据科学家 Agnis Liukis :在ML领域,初学者踩过的5个坑
[web penetration] detailed explanation of CSRF vulnerability









