Purchased is ali Cloud Lightweight application server Node.js version
To summarize each configuration and the problems encountered:
1. Preparation
- Packed front-end code, back-end Node code
Xshell, Xftp
The installation,Xshell
Create a session and log in to the Ali cloud server- Upload the background code node to the server /root directory. Install the MongoDB database in the server /root directory.
- Open the database in the same way as you would start the database locally (change the corresponding path)
// Enter Xftp according to the link contained in the following link to find, Understand the principle of known mongod -- dbpath = / usr/local/mongo/data/db - logpath = / usr/local/mongo/log/mongo. Log - fork / / Directing a json file is added to the database approach / / test library collection / / user name mongoimport - db test - collection user - the file/root/static/mock/city. The jsonCopy the code
Note two issues: the database identity and the new URL escape method
/ / app. Js/var/link database dataUrl = 'mongo: / / 120.79 XX. XX: 27017 / mall' / / data must be a string error, you need to specify the database id, DataUrl, {useNewUrlParser:true}, function (err, res) {useNewUrlParser:true} If (err){console.log(" Database connection failed! ); Console. log(err, "err")}else{console.log(" Database connection successful!" ); }});Copy the code
- Start the Node one-off service with Node.app. js (you can also start the automatic online service with pm2 start app.js)
2. Open the browser to access the front-end static file
HTTP protocol default port 80, so use node to listen on port 80, so that port 80 does not need to write, beautiful
http.listen(80, function(){ console.log('listening on *:80'); }); Get ('/', (req, res) => {res.redirect('/web/index.html')}) // Access the default port 80 directly to the desired file app.get('/', (req, res) => {res.redirect('/web/index.html')})Copy the code