I have a node server running right now, I use npm forever to keep it running when I'm not developing and I use npm nodemon for when I edit (restarts app on edit/upload).
I noticed that whenever I restart my app, session data is lost and my players have to re-login to their accounts. It's not a huuuge deal, but I was wondering if there was a way to edit my server.js page without restarting the app, and logging everybody out?
(Note that this only applies for server.js or module edits. .html and .js pages that are served don't require a restart)
(Second note: I am using mysql, nodejs, angularjs, express.io for all this, just in case anybody asks)
There isn't a way to edit a file (and have the changes loaded) without rebooting the server and reloading the file. Instead, store your session data somewhere other than memory. I know you're using MySQL, so use the connect-mysql package (there are other packages like this for redis, MongoDB, etc).
It's as simple as putting this in your app.js file:
var MySQLStore = require('connect-mysql')(express)
, options = {
config: {
host : 'place.stuff',
user: 'RUJordan',
password: 'hunter2',
database: 'SomeKittensIsGreat'
}
};
app.use(express.session({
secret: 'UpvoteThisAnswer',
store: new MySQLStore(options),
cookie: { maxAge: 2592000000 } // 30 days
}));
This will place a HTTP-only (i.e. the user can't see/use it) cookie on each user's computer. Even after a server reboot, connect-mysql will be able to like a user with their session data in MySQL via this cookie.
Related
I uploaded my repo and it has a database string named 'dbstring' which I do not want to share with anyone.
I created a repository secret on github and created a value named DBSTRING with its value but the thing is I dont know how to access it.
this is my uploaded code which reveals my dbstring.
const dbstring = mongodb+srv:/***********b.net
mongoose.connect(dbstring, { useUnifiedTopology: true, useNewUrlParser: true });
const db = mongoose.connection;
db.once('open', () => {
console.log('Database connected:', url);
});
How can I replace dbstring with secret value I created on my github repo?
What you need to do is to use Environment variables, where you can have a .env ( if you use dotenv ) for each environment. Then you keep your database credentials safe on your computer and on the server, this will also make it possible to target different environments like database production, dev, test, etc. Make sure you have .env file added in the .gitignore file.
It's also important that when you run this code it's executed on the server-side otherwise anyone with the dev tools open will be able to see the credentials as well. Then on your client side you make a request using axios to the URL related to that database connection.
If the ENV file works for you then what you can do is you can encrypt it before uploading it to the GitHub like creating an env-production file and encrypting it and once you use that repo you can decrypt it and you can also add that step to your CD/CI Line use this
In my application i have the client in one folder and the server in another. I successfully deployed the application to azure web apps, this can be found at: https://wulfdevpage.azurewebsites.net/ However, it takes nearly 15 seconds for the page to respond and render the client, (this is just the basic react-app client for now, but ill build this out further soon.) my problem is simply that it takes way to long for the server to respond with the application.
here is my folder structure
--client
- build
// - (other parts of the react app)
--server
- [other folders, like controllers, models etc.]
- server.js //entry point
in my server.js file this is how the build folder is served up.
// Set static folder
const __dirname = path.resolve();
app.use(express.static(path.join(__dirname, "public")));
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname, "/client/build")));
app.get("*", (req, res) =>
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"))
);
} else {
app.get("/", (req, res) => {
res.send("API is running...");
});
}
This works, but again, it's taking nearly 15 seconds from the initial request to the client reaching my computer to make this work. I know a simple solution would be to just move the client to something like azure static web apps but I really don't want to go this route, I'd rather keep them all in one place for convenience's sake. There simply must be a quicker way to serve up the client.
The major cause of performance of initial load issue is adding too many components into a single bundle file, so it takes more time to loading of that bundle files. To avoid this kind of issue, we need to structure our components in an optimized way.
To solve this react itself has a native solution, which is code-splitting and lazy loading. Which allows splitting bundle files into a smaller size. Refer here
The performance issue fix with some configuration changes. If you haven't done this already, please check below once
Enable HTTP/2
HTTP/2 brings major changes to our favorite web protocol, and many of the changes aim to improve performance and reduce latency on the web
Turn Off the Application Request Routing Cookie
Turn off the routing cookie in the Configuration blade to improve performance and resiliency.
Keep the App Service Always On
To prevent the idle shutdown, you can set the Always On flag in the App Service Configuration blade.
Use local cache
In App Setting create the app with a key of WEBSITE_LOCAL_CACHE_OPTION and a value of Always. You'll then have a d:\home folder pointing to a local cache on the machine and populated with a copy of your site content.
Use App Service diagnostic to fetch more details on the issue
Refer here for more info
I don't really have experience with backend and how things work together. I have created a simple live message sending app with node.js and socket.io. When I host a static web server on my machine (http-server which runs on local port using node.js) my app works perfectly fine but when I upload it on my host or github pages just for test, the backend doesn't seem to work. I uploaded all my files with an FTP program and the frontend loads fine but the backend doesn't. Do I have to know something like Django or ASP.NET to make these work on my host?
EDIT: One more thing, first line in my server.js is const io = require('socket.io')(3000)and in my script.js - const socket = io('http://localhost:3000')where 3000 and localhost:3000 stands for local host in my machine. What do i need to put instead of these?
You probably need to install and setup Node.js on your server, contact yout hosting provider for node installation if the option isn't available in yout cPanel.
My mongoDB connects just fine locally, then once I deploy to heroku, the connection URL becomes undefined. I have a .env file in my root directory, and a .gitignore which includes the .env. I need the mongo url to stay private, but I can only assume that this is why it shows as undefined in production...
Heres the error:
"MongooseError: The uri parameter to openUri() must be a string, got "undefined". Make sure the first parameter to mongoose.connect() or mongoose.createConnection() is a string."
Heres my require:
require("dotenv").config()
var url = process.env.MONGODB_URI
Heres my connection:
mongoose.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
}).catch(e => {
console.error(e.message)
})
Thanks in advance for the help.
Two things you need to do, Make that .env available for the production code, It can't access your local .env file.
Second is your mongo running on production ? mean availabe and accessible for the app through 27017 port ?
don't say it is running on your local machine.
You need to setup your variables where your app is deployed. It is not accessible from your .env file that you used while developing on your local host. If you deployed to heroku - here is a documentation of what to do. If it's not deployed to heroku, you can get an idea what to do next to make it work.
I'm developing a simple app with nodejs. The thing is that the first thing I do is to run a server.js file which loads an html form and checks if the entered information is valid.
After authentication, I'm planning to run another server.js (starting it from the original server.js file) in a subfolder for starting the real application.
Is this possible?
Definitely, check out child_process.spawn.
child_process.spawn("node", ["server.js"]);
process.exit(); // ends auth server