Make sure node and express are installed before you start
1. Initialization project
express ***(***Your project name) cd *** npm install(Download dependency)
Startup project: Run Command: node. / bin/www, port 3001 is my own change, in / bin/www (it is recommended to use WebStorm to run, because we need to use it to write code)
2. Coding
Open the project with WebStorm, and click the following command, which is the same as the command node. / bin / www
You can also add app.listen(8088) in aa.js and run http://127.0.0.1 : 8088 (Port Free write)
app.js (partial code)
var indexRouter = require('./routes/index'); var usersRouter = require('./routes/users'); app.use('/', indexRouter); // This means that to visit http://127.0.0.1:8088/users is to visit app.use('/users', usersRouter); app.listen(8088) module.exports = app;
Add the following code in routes/users to write an interface to obtain wechat oppenId through code
var http = require('https'); var url = require('url'); var appId = 'Your appaId'; var sessionKey = 'Your sessionKey'; /** * Get oppenId */ router.post('/getOppenId', function (req, res, next) { var data = { }; data = JSON.stringify(data); var opt = { // hostname:'https://api.weixin.qq.com', hostname: url.parse( 'https://api.weixin.qq.com').hostname, method:'get', path:`/sns/jscode2session?appid=${appId}&secret=${sessionKey}&js_code=${req.body.code}&grant_type=authorization_code`, headers:{ "Content-Type": 'application/json', "Content-Length": data.length } } let respon = res; var body = ''; var req = http.request(opt, function(res) { res.on('data',function(data){ body += data; }).on('end', function(){ respon.json({"data": JSON.parse(body)}); }); }).on('error', function(e) { console.log("error: " + e.message); }) req.write(data); req.end(); });
So you can get through http://127.0.0.1 : 8088/users/getOppenId got oppenId.
Be careful:
1. hostname cannot be used directly https://api.weixin.qq.com 'to use url.parse(' https://api.weixin.qq.com').hostname
2. The parameter to obtain oppenId must be spelled on the connection. Otherwise, an error will be reported: {"errcode":40013,"errmsg":"invalid appid"}
3. If var http = require('http ') is used, an error will appear: {"errCode": 43003, "errmsg": "require https hint: [gyw4pa0803re59]," expires_uin ": null}. I don't know whether the third-party interface that should be changed for me is https or what. I haven't tried
*In order to make another call directly, cross domain may appear. Add the following code in app.js to solve cross domain;
app.all('*',function (req, res, next) { res.header('Access-Control-Allow-Origin', '*'); res.header('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild'); res.header('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS'); if (req.method == 'OPTIONS') { // res.send(200); / let options request return quickly/ res.sendStatus(200) } else { next(); } });
The next sequelize operation database Link description
Xiaobai, spray lightly if there is a mistake, forget to point out