Make writing a habit together! This is my first day to participate in the “Gold Digging Day New Plan · April More text challenge”, click to see the details of the activity.
What is Tencent cloud service
Focus on the business itself, not the server, the environment, etc. Developers just write the code, push it to the cloud server, and it updates automatically.
1.1 Concepts advocated by Serveless
- Function as a Service, Fass;
- Backend is a service, Backend as a Serveice, Bass;
1.2 Advantages of Tencent cloud service
- No need to consider what physical machine/virtual machine, combined with the case of workflow, code submission automatic deployment, directly run;
- No server, maintenance book to greatly reduce, higher security and stability;
- Elastic scaling cloud, do not worry about performance issues;
- Most Serverless services charge by usage (e.g., traffic, CPU usage);
Brief introduction of short URL service functions
Usually visit a website, the back may carry a bunch of parameters, such a connection will be too long, short website is a good solution to this problem
2.1 Users can create corresponding short links for specified URLS
- Allows the user to specify the generated slug with parameters
- If no short chain is specified, the automatically generated short character ID is used by default
2.2 When users access the short link, the corresponding URL is automatically redirected
- If the short chain does not exist, 404 is returned
- Log access if short chain exists (IP, UA, timestamp)
3. Build the Node + Express environment
3.1 Project initialization
npm init -y
Copy the code
3.2 installation express
npm i express
Copy the code
3.3 Installing the Node-dev hot update code
npm i node-dev -D
Copy the code
3.4 Writing the Dev script
3.5 Writing app.js program
const express = require('express')
const app = express()
// Parse JSON format, X-www-form-urlencoded middleware
app.use(express.json())
app.use(express.urlencoded())
app.post('/create'.(req, res) = > {
console.log(req.body);
const { url, slug} = req.body
res.send({url,slug,date: Date.now()})
})
module.exports = app
Copy the code
3.6 Writing a program to start the Node Service
const app = require('./app')
app.listen(3080.() = > {
console.log('server is ready > http://localhost:3080');
})
Copy the code
3.7 Testing the Node + Express Environment
npm run dev
Copy the code
- Test /create requests using Postman
Combined with the content-type
Data is passed using json, the most common
Environment setup OK
Iv. Tencent Cloud service SDK was introduced
4.1 Search for cloudBase on Tencent Cloud Platform
4.2 Creating a Back-end Service (You can also Create a Back-end Service using a Blank Template)
4.3 Charging by volume
If a pop-up upgrade database 4.5 yuan can be directly returned, do not upgrade
4.4 Access the Database and create a Links database
- This screen has the database usage documentation, click to go to the node related
4.4 The Secretid and secretKey are generated for the first time
Cloud.tencent.com/document/pr…
4.5 Creating. Env Record ID and key
Haha, I will disable this later
4.6 Installing DotenV so that data in. Env can be read in the development environment
npm i dotenv -D
Copy the code
4.7 Writing startup Scripts
Package. json -r reads. Env first
"dev": "node-dev -r dotenv/config index.js"
Copy the code
4.8 Write the code to insert into Tencent cloud database
const express = require('express')
const { init } = require('@cloudbase/node-sdk')
const { SECRET_ID, SECRET_KEY } = process.env
const tcb = init({ env: 'hello-cloudbase-8gvwwq9l939dc1c2'.secretId: SECRET_ID, secretKey: SECRET_KEY })
const db = tcb.database()
const app = express()
// Parse JSON format, X-www-form-urlencoded middleware
app.use(express.json())
app.use(express.urlencoded())
app.post('/create'.async(req, res) => {
console.log(req.body);
const { url, slug } = req.body
await db.collection('links').add({slug,url})
res.send({link: `http://lcoalhost:3080/${slug}`})})module.exports = app
Copy the code
- test
- Successfully introduced Tencent cloud development SDKCopy the code
4.9 introduced nanoid
- npm i nanoid
Copy the code
Test the first short code link
5.1 Create short and long code links
const { nanoid} = require('nanoid')
// Store the corresponding link of the length code
app.post('/create'.async(req, res) => {
console.log(req.body);
// If no short code is passed, a 6-digit ID is automatically generated for short code
const { url, slug=nanoid(6) } = req.body
await db.collection('links').add({slug,url})
res.send({link: `http://lcoalhost:3080/${slug}`})})Copy the code
5.2 /: Slug dynamic route parameter takes long code link
// Find the long code according to the short code and redirect
app.get('/:slug'.async (req, res) => {
console.log(req.params);
const { slug } = req.params
const { data } = await db.collection('links').where({ slug }).get()
const [link] = data
res.redirect(link.url)
})
Copy the code
ok