我正在开发一个带有MEAN堆栈的Web应用程序,我想要这个场景:
人们可以注册,之后,他们必须至少注册一个company
当他们注册example.com/companyName/
example.com/companyName/members
example.com/companyName/settings
example.com/companyName/etc
时,基本URL将如下:
app.use('/'+companyName, routes);
所以,我的问题是,我如何在路线中使用变量来匹配用户的注册公司名称?
在app.js
中是否可以使用app.use(function (req, res, next) {
console.log('going to '+companyUrl);
next();
});
app.use('/'+companyUrl, routes);
?
我的代码中有一段,以防万一:
noStartupRestrict.theStartupUrl
我正在其他模块上做一些工作,现在,app
应该是 companyName 。
但是import java.io.IOException;
import java.util.Arrays;
import java.util.Set;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientNetworkConfig;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.config.SerializationConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.DataSerializableFactory;
import com.hazelcast.nio.serialization.HazelcastSerializationException;
import com.hazelcast.nio.serialization.IdentifiedDataSerializable;
/**
*
* @author igor.masternoy
*
*/
@RunWith(BlockJUnit4ClassRunner.class)
public class HZSerialTest {
private static final int FACROTY_ID = 42;
private static final int ENTITY_ID = 42;
private static final String WINDOWS_HZ_SERVER_IP = "###.###.###.###";
private static final String TEST_FIELD = "CHESTERFIELD";
private static final String TEST_COLLECTION = "CHESTERFIELD";
@Test(expected = HazelcastSerializationException.class)
public void test() {
ClientConfig clientConfig = new ClientConfig();
ClientNetworkConfig networkConfig = clientConfig.getNetworkConfig();
String[] serverAddresses = new String[] { WINDOWS_HZ_SERVER_IP };
// logger.info("Hazelcast servers IPs {" + Arrays.toString(serverAddresses) + "}");
networkConfig.addAddress(serverAddresses);
networkConfig.setSmartRouting(true);
networkConfig.setConnectionTimeout(30000);
clientConfig.setGroupConfig(new GroupConfig("dev",
"dev-pass"));
SerializationConfig serializationConfig = clientConfig.getSerializationConfig();
serializationConfig
.addDataSerializableFactory(FACROTY_ID, new TestObjectSerializationFactory());
clientConfig.setSerializationConfig(serializationConfig);
HazelcastInstance instance = HazelcastClient.newHazelcastClient(clientConfig);
Set<TestEntity> testSet = instance.getSet(TEST_COLLECTION);
testSet.add(new TestEntity(1, 1, TEST_FIELD));
testSet = instance.getSet(TEST_COLLECTION);
IMap<String, TestEntity> map = instance.getMap("test.map");
map.put("key", new TestEntity(1, 1, TEST_FIELD));
TestEntity entity = testSet.iterator().next();
// logger.info("{}", entity);
entity = map.get("key");
// logger.info("{}", entity);
}
class TestObjectSerializationFactory implements DataSerializableFactory {
@Override
public IdentifiedDataSerializable create(int typeId) {
IdentifiedDataSerializable obj = null;
switch (typeId) {
case 42:
obj = new TestEntity();
break;
default:
// logger.error("Wow wo?w wow not a case");
break;
}
return obj;
}
}
class TestEntity implements IdentifiedDataSerializable {
private String field;
private int field2;
private int field3;
public TestEntity() {
}
public TestEntity(int field2,
int field3,
String field) {
super();
this.field = field;
}
@Override
public void readData(ObjectDataInput in) throws IOException {
field = in.readUTF();
field2 = in.readInt();
field3 = in.readInt();
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
out.writeUTF(field);
out.writeInt(field2);
out.writeInt(field3);
}
@Override
public int getFactoryId() {
return FACROTY_ID;
}
@Override
public int getId() {
return ENTITY_ID;
}
@Override
public String toString() {
return "TestEntity [field=" + field + field2 + field3 + "]";
}
}
}
没有使用路由。它一直到404路线。
答案 0 :(得分:0)
你试过吗?
app.js
...
var path = '/companyName';
app.use(path, routes);
...
答案 1 :(得分:0)
试试这个
app.use('/:companyName/something', function(req, res, next) {
console.log(req.params.companyName);
next();
})